% 横坐标 -10 to 20 n = -10:20; % 纵坐标,与横坐标相对应 x = [zeros(1,10) 1zeros(1,20)]; % 生成图像 stem(n,x); xlabel('n');ylabel('\delta[n]');
时域线性卷积
1 2 3 4 5 6 7
h = [321-210-403]; % 脉冲响应 x = [1-23-4321]; % 输入信号 y = conv(h,x); %h[n],x[n]都为有限长序列,且在线性时不变离散系统,求输出序列 n = 0:14; %输出序列的时间维度,9+7-1 stem(n,y); xlabel('n'); ylabel('y[n]'); title('Output Obtained by Linear Convolution'); grid;
L=length(x); N=L; % set samples number in frequency domain w=2*pi/N*(0:N-1); % discrete frequency %DFT Xw=zeros(1,N); % vector for storing DFT for k=1:N Xw(k)=x*(exp(-j*w(k)*(0:L-1)')); end
%IDFT y=zeros(1,N); for n=1:N y(n)=(1/N)*Xw* (exp(j*(n-1)*w')); end
圆周移位
1 2 3 4 5 6 7 8 9
functiony=cirshift(x,M) % M为序列圆周移位的样本数 %程序对一个有限长序列x圆周移位M个采样间隔得到新序列y ifabs(M)>length(x) M=rem(M,length(x)); end if M<0 M=M+length(x); end y=[x(M+1:length(x)) x(1:M)];
圆周卷积
1 2 3 4 5 6 7 8 9 10 11 12 13
functiony=circonv(x1,x2); L1=length(x1); L2=length(x2); if L1 ~=L2 error('Sequences of unequal lengths') end y=zeros(1,L1); x2tr=[x2(1) x2(L2:-1:2)]; for k=1:L1 sh=cirshift(x2tr,1-k); h=x1.*sh; y(k)=sum(h); end
圆周卷积求线性卷积(时域)
条件:圆周卷积的长度大于线性卷积长度
1 2 3 4 5 6 7 8 9
g1 = [12345];g2 = [22011]; g1e = [g1 zeros(1,length(g2)-1)]; g2e = [g2 zeros(1,length(g1)-1)]; ylin = circonv(g1e,g2e); disp('Linear convolution via circular convolution = '); disp(ylin); y = conv(g1, g2); disp('Direct linear convolution = '); disp(y)