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

by admin on 2020年1月27日

RS编码的matlab仿真(m文件仿真)
对加入突发噪声信道的RS(15,11)编码进行的仿真:代码如下:

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

     
LMS算法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;
t = n*1000/128/fs;
%plot;xlabel(‘time in ms’);

  
 LMS自适应滤波器是使滤波器的输出信号与期望响应之间的误差的均方值为最小,因此称为最小均方(LMS)自适应滤波器。其原理及推导见

L=1000;

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

澳门新葡亰平台官网 1function [yn,W,en]=LMS(xn,dn,M,mu,itr)
澳门新葡亰平台官网 2% LMS(Least Mean Squre)算法
澳门新葡亰平台官网 3% 输入参数:
澳门新葡亰平台官网 4%     xn   输入的信号序列      (列向量)
澳门新葡亰平台官网 5%     dn   所期望的响应序列    (列向量)
澳门新葡亰平台官网 6%     M    滤波器的阶数        (标量)
澳门新葡亰平台官网 7%     mu   收敛因子(步长)      (标量)     要求大于0,小于xn的相关矩阵最大特征值的倒数    
澳门新葡亰平台官网 8%     itr  迭代次数            (标量)     默认为xn的长度,M<itr<length(xn)
澳门新葡亰平台官网 9% 输出参数:
澳门新葡亰平台官网 10%     W    滤波器的权值矩阵     (矩阵)
澳门新葡亰平台官网 11%          大小为M x itr,
澳门新葡亰平台官网 12%     en   误差序列(itr x 1)    (列向量)  
澳门新葡亰平台官网 13%     yn   实际输出序列             (列向量)
澳门新葡亰平台官网 14
澳门新葡亰平台官网 15% 参数个数必须为4个或5个
澳门新葡亰平台官网 16if nargin == 4                 % 4个时递归迭代的次数为xn的长度 
澳门新葡亰平台官网 17    itr = length(xn);
澳门新葡亰平台官网 18elseif nargin == 5             % 5个时满足M<itr<length(xn)
澳门新葡亰平台官网 19    if itr>length(xn) | itr<M
澳门新葡亰平台官网 20        error(‘迭代次数过大或过小!’);
澳门新葡亰平台官网 21    end
澳门新葡亰平台官网 22else
澳门新葡亰平台官网 23    error(‘请检查输入参数的个数!’);
澳门新葡亰平台官网 24end
澳门新葡亰平台官网 25
澳门新葡亰平台官网 26
澳门新葡亰平台官网 27% 初始化参数
澳门新葡亰平台官网 28en = zeros(itr,1);             % 误差序列,en(k)表示第k次迭代时预期输出与实际输入的误差
澳门新葡亰平台官网 29W  = zeros(M,itr);             % 每一行代表一个加权参量,每一列代表-次迭代,初始为0
澳门新葡亰平台官网 30
澳门新葡亰平台官网 31% 迭代计算
澳门新葡亰平台官网 32for k = M:itr                  % 第k次迭代
澳门新葡亰平台官网 33    x = xn(k:-1:k-M+1);        % 滤波器M个抽头的输入
澳门新葡亰平台官网 34    y = W(:,k-1).’ * x;        % 滤波器的输出
澳门新葡亰平台官网 35    en(k) = dn(k) – y ;        % 第k次迭代的误差
澳门新葡亰平台官网 36    
澳门新葡亰平台官网 37    % 滤波器权值计算的迭代式
澳门新葡亰平台官网 38    W(:,k) = W(:,k-1) + 2*mu*en(k)*x;
澳门新葡亰平台官网 39end
澳门新葡亰平台官网 40
澳门新葡亰平台官网 41% 求最优时滤波器的输出序列
澳门新葡亰平台官网 42yn = inf * ones(size(xn));
澳门新葡亰平台官网 43for k = M:length(xn)
澳门新葡亰平台官网 44    x = xn(k:-1:k-M+1);
澳门新葡亰平台官网 45    yn(k) = W(:,end).’* x;
澳门新葡亰平台官网 46end
澳门新葡亰平台官网 47    

M=4;%每个符号的比特数

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

        LMS函数的一个实例:

N=2^M-1;%编码后码字长度

%First Filter
upFilter1 = decompose(filter1,L1);
澳门新葡亰平台官网 ,downFilter1 = decompose(filter1,M1);

澳门新葡亰平台官网 48%function main()
澳门新葡亰平台官网 49close  all
澳门新葡亰平台官网 50
澳门新葡亰平台官网 51% 周期信号的产生 
澳门新葡亰平台官网 52t=0:99;
澳门新葡亰平台官网 53xs=10*sin(0.5*t);
澳门新葡亰平台官网 54figure;
澳门新葡亰平台官网 55subplot(2,1,1);
澳门新葡亰平台官网 56plot(t,xs);grid;
澳门新葡亰平台官网 57ylabel(‘幅值’);
澳门新葡亰平台官网 58title(‘it{输入周期性信号}’);
澳门新葡亰平台官网 59
澳门新葡亰平台官网 60% 噪声信号的产生
澳门新葡亰平台官网 61randn(‘state’,sum(100*clock));
澳门新葡亰平台官网 62xn=randn(1,100);
澳门新葡亰平台官网 63subplot(2,1,2);
澳门新葡亰平台官网 64plot(t,xn);grid;
澳门新葡亰平台官网 65ylabel(‘幅值’);
澳门新葡亰平台官网 66xlabel(‘时间’);
澳门新葡亰平台官网 67title(‘it{随机噪声信号}’);
澳门新葡亰平台官网 68
澳门新葡亰平台官网 69% 信号滤波
澳门新葡亰平台官网 70xn = xs+xn;
澳门新葡亰平台官网 71xn = xn.’ ;   % 输入信号序列
澳门新葡亰平台官网 72dn = xs.’ ;   % 预期结果序列
澳门新葡亰平台官网 73M  = 20   ;   % 滤波器的阶数
澳门新葡亰平台官网 74
澳门新葡亰平台官网 75rho_max = max(eig(xn*xn.’));   % 输入信号相关矩阵的最大特征值
澳门新葡亰平台官网 76mu = rand()*(1/rho_max)   ;    % 收敛因子 0 < mu < 1/rho
澳门新葡亰平台官网 77
澳门新葡亰平台官网 78[yn,W,en] = LMS(xn,dn,M,mu);
澳门新葡亰平台官网 79
澳门新葡亰平台官网 80% 绘制滤波器输入信号
澳门新葡亰平台官网 81figure;
澳门新葡亰平台官网 82subplot(2,1,1);
澳门新葡亰平台官网 83plot(t,xn);grid;
澳门新葡亰平台官网 84ylabel(‘幅值’);
澳门新葡亰平台官网 85xlabel(‘时间’);
澳门新葡亰平台官网 86title(‘it{滤波器输入信号}’);
澳门新葡亰平台官网 87
澳门新葡亰平台官网 88% 绘制自适应滤波器输出信号
澳门新葡亰平台官网 89subplot(2,1,2);
澳门新葡亰平台官网 90plot(t,yn);grid;
澳门新葡亰平台官网 91ylabel(‘幅值’);
澳门新葡亰平台官网 92xlabel(‘时间’);
澳门新葡亰平台官网 93title(‘it{自适应滤波器输出信号}’);
澳门新葡亰平台官网 94
澳门新葡亰平台官网 95% 绘制自适应滤波器输出信号,预期输出信号和两者的误差
澳门新葡亰平台官网 96figure 
澳门新葡亰平台官网 97plot(t,yn,’b’,t,dn,’g’,t,dn-yn,’r’);grid;
澳门新葡亰平台官网 98legend(‘自适应滤波器输出’,’预期输出’,’误差’);
澳门新葡亰平台官网 99ylabel(‘幅值’);
澳门新葡亰平台官网 100xlabel(‘时间’);
澳门新葡亰平台官网 101title(‘it{自适应滤波器}’);

K=N-4;%信息长度

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

       运行后的结果如下:

MSG=randint(L,1);%随机产生L比特信号

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

澳门新葡亰平台官网 102

TP=gftuple([-1:N-1]’,M);%产生加罗华域元素

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

澳门新葡亰平台官网 103

PG=rspoly(N,K);%产生生成式

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

澳门新葡亰平台官网 104

[CODE,ADDED]=rsenco(MSG,TP,K);%编码

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

NOI=rand(length(CODE)/M,1)<.03;%加入3%的噪声

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

NOI=(NOI*ones(1,M))’;%产生突发错误

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

NOI=NOI(:);

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

CODE_NOI=rem(CODE+NOI,2);%噪声加入信号

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

[DEC,ERR,CCODE,ERR_C]=rsdeco(CODE_NOI,TP,K);%译码

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

MSG=[MSG;zeros(ADDED,1)];%调整长度

output3 = output3/max;
lout = length;

max(abs(DEC-MSG));%比较

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

X=[1:length(NOI)];

figure
subplot;plot;title(‘True Signal’);
subplot;plot;title(‘Resampled Signal’);
subplot;plot;title;

Z=[1:M*N:length(NOI)];

figure
subplot;plot;title(‘True Signal’);
subplot;plot;title(‘Built-in Resampled Signal’);
subplot;plot;title;

Y=zeros(1,length(Z));

function x_decompose = decompose
lx = length;

Z=[Z;Z];

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

Y=[Y+min(ERR_C);Y+max(ERR_C)];

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

subplot(211);

end

plot(X,NOI,’yo’,X,ERR_C,’rx’,Z,Y,’g-‘);

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

title(‘Error Detection Record’);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%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);

xlabel(‘o–placed error;x–detected error;vertical bar: RS-DECO
section.’);

%%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);

axis([1,length(NOI),min(ERR_C),max(ERR_C)]);

%%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);

X=[1:length(MSG)];                  

Z=[1:M*K:length(MSG)];

Y=zeros(1,length(Z));

Z=[Z;Z];

Y=[Y;Y+max(MSG)];

subplot(212);

plot(X,MSG,’yo’,X,DEC,’rx’,Z,Y,’g-‘);

title(‘Message and Decoded Signal Comparison’);

xlabel(‘o–original message;x–decoded result.’);

axis([1,length(MSG),min(min(MSG)),max(max(MSG))]);

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图