目录
一、用MATLAB编写DFS计算程序
The syntax is ‘y=dfs(x, k)’, where the output vector ‘y’ represents the Fourier coefficients Ck , the input vector ‘x’ represents one period of x[n], and the parameter vector ‘k’ is the indexes in Ck .
Use your ‘dfs’ function to calculate the DFS of,then use the same ‘dfs’ function to perform the inverse transform, the result should be equal to .
答案如下所示:
N=6; n=0:N-1;
xn =2*cos(2*pi/3*n)+cos(pi/3*n)
y=dfs(xn,n); %x[n]的dfs结果
x1n=dfs(y/N,-n); %利用函数dfs求解x[n]
amX=abs(y);angX=angle(y); %求解幅度及相位
angX(amX<0.001)=0; %将幅度为0的相位置零
angX(angX<0.001)=0; %将很小的相位置零
function y=dfs(x,k) %dfs函数实现
N=length(k);
n=0:N-1;
y=x*exp(-j*2*pi/N).^(n'*k);
end
二、寻找DFT和DTFT之间的关系
A nonperiodic sequence ,use your own ‘dtft’ function to find out the spectrum. A periodic sequence is the periodic extension of . ,use your ‘dfs’ function to find out the spectrum.
Analyze the relationship between the two spectra, including magnitude and phase spectra.
We find that ,which proves the validity of the plotting.
x=[1 1 1 1];
y1=dtft(x,[0 1 2 3]);
x2n=[1 1 1 1 0 0 0 0]
y2=dfs(x2n,[0:7]);
figure(1)
magX1=abs(y1); %绘制x(n)的幅度谱
subplot(2,1,1);plot([0:2*pi/3999:2*pi],magX1,'b');
hold on;
magX2=abs(y2); %绘制x(n)的幅度谱
subplot(2,1,1);stem([0:2*pi/8:2*pi/8*7],magX2);
xlabel('\omega'),ylabel('|\itX\rm(\omega)|');title('Amplitude spectrum');
axis([0 2*pi 0 4]);
%set(gca,'xtickmode','manual','xtick',[0,"4\pi"]);
set(gca,'xtick',[ 0 0.5*pi pi 1.5*pi 2*pi]);
xticklabels({'0','0.5\pi','\pi','1.5\pi','2\pi'})
angX1=angle(y1); %绘制x(n)的相位谱
subplot(2,1,2);plot([0:2*pi/3999:2*pi],angX1,'b');
hold on;
angX2=angle(y2); %绘制x(n)的相位谱
subplot(2,1,2);stem([0:2*pi/8:2*pi/8*7],angX2) ;xlabel('\omega'),ylabel('\it\theta\rm(\omega)'); title ('Phase spectrum');
axis([0 2*pi -3 3]);
set(gca,'xtick',[ 0 0.5*pi pi 1.5*pi 2*pi]);
xticklabels({'0','0.5\pi','\pi','1.5\pi','2\pi'})
三、备注 DFS和DTFT求解公式
function y=dfs(x,k)
N=length(k);
n=0:N-1
y=x*exp(-j*2*pi/N).^(n'*k);
end
function y=dtft(x,n)
w=linspace(0,2*pi,4000);
y=x*exp(-j*n'*w);
end