数字信号处理实验线性卷积与循环卷积计算
数字信号处理实验线性卷积与循环卷积计算
clc
clear
xn=[1 2 3 4 5] %生成x(n)
hn=[1 2 1 2]; %生成h(n)
N1=length(xn);
N2=length(hn);
yln=conv(xn,hn); %直接用函数conv计算线性卷积
ycn=circonv(xn,hn,N1);//N1可改为几点循环卷积如题目中改为6 8 10
ny1=[0:1:length(yln)-1];
ny2=[0:1:length(ycn)-1];
subplot(2,1,1); %画图
stem(ny1,yln);
xlabel('n')
ylabel('线性卷积')
subplot(2,1,2);
stem(ny2,ycn);
xlabel('n')
ylabel('循环卷积')function yc=circonv(x1,x2,N)
%realize circular convolution use direct method
%y=circonv(x1,x2,N)
%y:output sequences
%x1,x2:input sequences
%N:circulation length
if length(x1)>Nerror("N must not be less than length of x1");
end
if length(x2)>Nerror("N must not be less than length of x2");
end
%以上语句判断两个序列的长度是否小于N
x1=[x1,zeros(1,N-length(x1))]; %填充序列x1(n)使其长度为N1+N2-1(序列%h(n)的长度为N1,序列x(n)的长度为N2)
x2=[x2,zeros(1,N-length(x2))]; %填充序列x2(n)使其长度为N1+N2-1
n=[0:1:N-1];
x2=x2(mod(-n,N)+1); %生成序列x2((-n))N
H=zeros(N,N);
for n=1:1:NH(n,:)=cirshiftd(x2,n-1,N); %该矩阵的k行为x2((k-1-n))N
end
yc=x1*H';
end%计算循环卷
function y=cirshiftd(x,m,N)
%directly realize circular shift for sequence x
%y=cirshiftd(x,m,N);
%x:input sequence whose length is less than N
%m:how much to shift
%N:circular length
%y:output shifted sequence
if length(x)>Nerror('length of x must be less than N');
end
x=[x,zeros(1,N-length(x))];
n=[0:1:N-1];
y=x(mod(n-m,N)+1);
end
最新文章