关于重采样的问题,刚学很多不懂

自己在matlab里面编的一个重采样程序,但是效果很差,而且输出前一段数据有错,有没有人能帮我看看?

%input data
fa = 8000; %%signal frequency
fs = 44100; %%44.1kHz sampling frequency
n = 1:64;
x = sin(2*pi*fa*n/fs);
lengthx = length(x);
t = n*1000/128/fs;
%plot(t,x);xlabel('time in ms');

% [y fs nbits] = wavread('acoustic.wav');
% x = y';
% lx = length(x);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Resample parameters
L1 = 8;
M1 = 7;
L2 = 4;
M2 = 3;
L3 = 10;
M3 = 7;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%filters
load filter.mat
filter1 = b1;
filter2 = b2;
filter3 = b3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Initialize the subfilters

%First Filter
upFilter1 = decompose(filter1,L1);
downFilter1 = decompose(filter1,M1);

%Second Filter
upFilter2 = decompose(filter2,L2);
downFilter2 = decompose(filter2,M2);

%Second Filter
upFilter3 = decompose(filter3,L3);
downFilter3 = decompose(filter3,M3);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Polyphase Filtering

%First Filter
%
%%upsampling
intermediatex1 = zeros(L1,length(x));
x11 = zeros(L1,length(x)*L1);
for i = 1:L1
intermediatex1(i,:) = filter(upFilter1(i,:),1,x); %%do the filtering before upsampling
x11(i,:) = upsample(intermediatex1(i,:),L1); %%upsample by L, inserting L-1 zeros.
end

X1 = zeros(1,length(x)*L1);
for i = 1:length(x)
X1(L1*(i-1)+1:L1*(i-1)+L1) = x11(:,(i-1)*L1+1)';
end
%%downsampling
X1_down = [X1 zeros(1,M1-mod(length(X1),M1))];
output1 = zeros(1,length(X1_down)/M1);
x1down = zeros(M1,length(X1_down)/M1);
x11down = x1down;
for i = 1:M1
for k = 1:length(X1_down)/M1
x1down(i,k) = X1_down(M1*(k-1)+i);
end
x11down(i,:) = filter(downFilter1(1,:),1,x1down(i,:));
output1 = output1 + x11down(i,:);
end

%Second Filter
%
%%upsampling
intermediatex2 = zeros(L2,length(output1));
x22 = zeros(L2,length(output1)*L2);
for i = 1:L2
intermediatex2(i,:) = filter(upFilter2(i,:),1,output1); %%do the filtering before upsampling
x22(i,:) = upsample(intermediatex2(i,:),L2); %%upsample by L, inserting L-1 zeros.
end

X2 = zeros(1,length(output1)*L2);
for i = 1:length(output1)
X2(L2*(i-1)+1:L2*(i-1)+L2) = x22(:,(i-1)*L2+1)';
end
%%downsampling
X2_down = [X2 zeros(1,M2-mod(length(X2),M2))];
output2 = zeros(1,length(X2_down)/M2);
x2down = zeros(M2,length(X2_down)/M2);
x22down = x2down;
for i = 1:M2
for k = 1:length(X2_down)/M2
x2down(i,k) = X2_down(M2*(k-1)+i);
end
x22down(i,:) = filter(downFilter2(1,:),1,x2down(i,:));
output2 = output2 + x22down(i,:);
end

%Third Filter
%
%%upsampling
intermediatex3 = zeros(L3,length(output2));
x33 = zeros(L3,length(output2)*L3);
for i = 1:L3
intermediatex3(i,:) = filter(upFilter3(i,:),1,output2); %%do the filtering before upsampling
x33(i,:) = upsample(intermediatex3(i,:),L3); %%upsample by L, inserting L-1 zeros.
end

X3 = zeros(1,length(output2)*L3);
for i = 1:length(output2)
X3(L3*(i-1)+1:L3*(i-1)+L3) = x33(:,(i-1)*L3+1)';
end
%%downsampling
X3_down = [X3 zeros(1,M3-mod(length(X3),M3))];
output3 = zeros(1,length(X3_down)/M3);
x3down = zeros(M3,length(X3_down)/M3);
x33down = x3down;
for i = 1:M3
for k = 1:length(X3_down)/M3
x3down(i,k) = X3_down(M3*(k-1)+i);
end
x33down(i,:) = filter(downFilter3(1,:),1,x3down(i,:));
output3 = output3 + x33down(i,:);
end

output3 = output3/max(output3);
lout = length(output3);

nx = 1:length(output3);
xorigin = sin(2*pi*fa*nx/96000);
error1 = xorigin - output3;
yin = [0 0 resample(x,320,147)];
error2 = xorigin - yin;

figure(1)
subplot(3,1,1);plot(xorigin);title('True Signal');
subplot(3,1,2);plot(output3);title('Resampled Signal');
subplot(3,1,3);plot(error1);title('Error');

figure(2)
subplot(3,1,1);plot(xorigin);title('True Signal');
subplot(3,1,2);plot(yin);title('Built-in Resampled Signal');
subplot(3,1,3);plot(error2);title('Error');


function x_decompose = decompose(x,factor)
lx = length(x);

if mod(lx,factor) ~= 0;
X = [x zeros(1,factor-mod(lx,factor))];
else
X = x;
end

x_decompose = zeros(factor,length(X)/factor);
for i = 1:factor
for k = 1:length(X)/factor
x_decompose(i,k) = X(factor*(k-1)+i);
end
end

end


%Filter Generation
fs = 44100;
L1 = 8;
M1 = 7;
L2 = 4;
M2 = 3;
L3 = 10;
M3 = 7;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%First Filter
f1 = [20000 24000];
a1 = [1 0];
dev1 = [0.01 10^-8];
fs1 = fs*L1;
[n1 fo1 ao1 w1] = firpmord(f1,a1,dev1,fs1);
b1 = firpm(n1,fo1,ao1,w1);

%%Second Filter
f2 = [20160 30240];
a2 = [1 0];
dev2 = [0.01 10^-8];
fs2 = fs1*L2/M1;
[n2 fo2 ao2 w2] = firpmord(f2,a2,dev2,fs2);
b2 = firpm(n2,fo2,ao2,w2);

%%Third Filter
f3 = [16800 50400];
a3 = [1 0];
dev3 = [0.01 10^-8];
fs3 = fs2*L3/M2;
[n3 fo3 ao3 w3] = firpmord(f3,a3,dev3,fs3);
b3 = firpm(n3,fo3,ao3,w3);


查看全部
zlmaritn
zlmaritn
2015/09/11 02:47
  • matlab
  • 重采样
  • 点赞
  • 收藏
  • 回答
    私信

0个回复