【 MATLAB 】使用 MATLAB 作图讨论有限长序列的 N 点 DFT(强烈推荐)(含MATLAB脚本)

举报
李锐博恩 发表于 2021/07/15 05:15:16 2021/07/15
【摘要】 这篇博文本来是和上篇博文一起写的:【 MATLAB 】离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间的关系 但是这篇博文我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇博文全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇博文之前还是先看看上篇博文。 我默认你已经看了上篇博文。 本博文的讨论建立在一个案例的基础上: 设x(n)是4点序列为: ...

这篇博文本来是和上篇博文一起写的:【 MATLAB 】离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间的关系

但是这篇博文我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇博文全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇博文之前还是先看看上篇博文。

我默认你已经看了上篇博文。


本博文的讨论建立在一个案例的基础上:

设x(n)是4点序列为:

x(n) = \left\{\begin{matrix} 1, &0 \leq n \leq 3 \\0, & else \end{matrix}\right.

计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)


我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.

DFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N.

x(n)的波形图为:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. stem(n,x);
  7. title('Discrete sequence');
  8. xlabel('n');ylabel('x(n)');
  9. xlim([0,5]);ylim([-0.2,1.2]);

我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. stem(n,x);
  7. title('Discrete sequence');
  8. xlabel('n');ylabel('x(n)');
  9. xlim([0,5]);ylim([-0.2,1.2]);
  10. %Discrete-time Fourier Transform
  11. K = 500;
  12. k = 0:1:K;
  13. w = 2*pi*k/K; %plot DTFT in [0,2pi];
  14. X = x*exp(-j*n'*w);
  15. % w = [-fliplr(w),w(2:K+1)]; %plot DTFT in [-pi,pi]
  16. % X = [fliplr(X),X(2:K+1)]; %plot DTFT in [-pi,pi]
  17. magX = abs(X);
  18. angX = angle(X)*180/pi;
  19. figure
  20. subplot(2,1,1);
  21. plot(w/pi,magX);
  22. title('Discrete-time Fourier Transform in Magnitude Part');
  23. xlabel('w in pi units');ylabel('Magnitude of X');
  24. subplot(2,1,2);
  25. plot(w/pi,angX);
  26. title('Discrete-time Fourier Transform in Phase Part');
  27. xlabel('w in pi units');ylabel('Phase of X ');

关于DTFT的编程案例,之前博文也有:【 MATLAB 】模拟信号采样及离散时间傅里叶变换(DTFT)案例分析


当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. %Discrete-time Fourier Transform
  7. K = 500;
  8. k = 0:1:K;
  9. w = 2*pi*k/K; %plot DTFT in [0,2pi];
  10. X = x*exp(-j*n'*w);
  11. magX = abs(X);
  12. angX = angle(X)*180/pi;
  13. %DFT in N = 4
  14. k = 0:N-1;
  15. Xk = dft(x,N);
  16. magXk = abs(Xk);
  17. phaXk = angle(Xk) * 180/pi; %angle 。
  18. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  19. figure
  20. subplot(2,1,1);
  21. stem(k/pi,magXk);
  22. title('DFT Magnitude of x(n) when N = 4');
  23. xlabel('k');ylabel('Magnitude Part');
  24. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  25. hold on
  26. plot(w/pi,magX,'g');
  27. hold off
  28. subplot(2,1,2);
  29. stem(k/pi,phaXk);
  30. title('DFT Phase of x(n) when N = 4');
  31. xlabel('k');ylabel('Phase Part');
  32. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  33. hold on
  34. plot(w/pi,angX,'g');
  35. hold off

可见,DFT是DTFT上的等间隔采样点。


当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. %Discrete-time Fourier Transform
  7. K = 500;
  8. k = 0:1:K;
  9. w = 2*pi*k/K; %plot DTFT in [0,2pi];
  10. X = x*exp(-j*n'*w);
  11. magX = abs(X);
  12. angX = angle(X)*180/pi;
  13. % Zero padding into N = 8 and extended cycle
  14. N = 8;
  15. x = [1,1,1,1,zeros(1,4)];
  16. %DFT in N = 8
  17. k = 0:N-1;
  18. Xk = dft(x,N);
  19. magXk = abs(Xk);
  20. phaXk = angle(Xk) * 180/pi; %angle
  21. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  22. figure
  23. subplot(2,1,1);
  24. stem(k/pi,magXk);
  25. title('DFT Magnitude of x(n) when N = 8');
  26. xlabel('k');ylabel('Magnitude Part');
  27. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  28. hold on
  29. plot(w/pi,magX,'g');
  30. hold off
  31. subplot(2,1,2);
  32. stem(k/pi,phaXk);
  33. title('DFT Phase of x(n) when N = 8');
  34. xlabel('k');ylabel('Phase Part');
  35. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  36. hold on
  37. plot(w/pi,angX,'g');
  38. hold off

相比于N =4,N =8采样点数更密集了。


当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. %Discrete-time Fourier Transform
  7. K = 500;
  8. k = 0:1:K;
  9. w = 2*pi*k/K; %plot DTFT in [0,2pi];
  10. X = x*exp(-j*n'*w);
  11. magX = abs(X);
  12. angX = angle(X)*180/pi;
  13. % Zero padding into N = 16 and extended cycle
  14. N = 16;
  15. x = [1,1,1,1,zeros(1,12)];
  16. %DFT in N = 16
  17. k = 0:N-1;
  18. Xk = dft(x,N);
  19. magXk = abs(Xk);
  20. phaXk = angle(Xk) * 180/pi; %angle
  21. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  22. figure
  23. subplot(2,1,1);
  24. stem(k/pi,magXk);
  25. title('DFT Magnitude of x(n) when N = 16');
  26. xlabel('k');ylabel('Magnitude Part');
  27. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  28. hold on
  29. plot(w/pi,magX,'g');
  30. hold off
  31. subplot(2,1,2);
  32. stem(k/pi,phaXk);
  33. title('DFT Phase of x(n) when N = 16');
  34. xlabel('k');ylabel('Phase Part');
  35. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  36. hold on
  37. plot(w/pi,angX,'g');
  38. hold off


N = 128的情况:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. %Discrete-time Fourier Transform
  7. K = 500;
  8. k = 0:1:K;
  9. w = 2*pi*k/K; %plot DTFT in [0,2pi];
  10. X = x*exp(-j*n'*w);
  11. magX = abs(X);
  12. angX = angle(X)*180/pi;
  13. % Zero padding into N = 128 and extended cycle
  14. N = 128;
  15. x = [1,1,1,1,zeros(1,124)];
  16. %DFT in N = 128
  17. k = 0:N-1;
  18. Xk = dft(x,N);
  19. magXk = abs(Xk);
  20. phaXk = angle(Xk) * 180/pi; %angle
  21. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  22. figure
  23. subplot(2,1,1);
  24. stem(k/pi,magXk);
  25. title('DFT Magnitude of x(n) when N = 128');
  26. xlabel('k');ylabel('Magnitude Part');
  27. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  28. hold on
  29. plot(w/pi,magX,'g');
  30. hold off
  31. subplot(2,1,2);
  32. stem(k/pi,phaXk);
  33. title('DFT Phase of x(n) when N = 128');
  34. xlabel('k');ylabel('Phase Part');
  35. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  36. hold on
  37. plot(w/pi,angX,'g');
  38. hold off

可见,随着周期N 的增大,采样点越密集。



上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:


  
  1. clc;clear;close all;
  2. % x(n)
  3. N = 4;
  4. n = 0:N-1;
  5. x = [1,1,1,1];
  6. stem(n,x);
  7. title('Discrete sequence');
  8. xlabel('n');ylabel('x(n)');
  9. xlim([0,5]);ylim([-0.2,1.2]);
  10. %Discrete-time Fourier Transform
  11. K = 500;
  12. k = 0:1:K;
  13. w = 2*pi*k/K; %plot DTFT in [0,2pi];
  14. X = x*exp(-j*n'*w);
  15. % w = [-fliplr(w),w(2:K+1)]; %plot DTFT in [-pi,pi]
  16. % X = [fliplr(X),X(2:K+1)]; %plot DTFT in [-pi,pi]
  17. magX = abs(X);
  18. angX = angle(X)*180/pi;
  19. figure
  20. subplot(2,1,1);
  21. plot(w/pi,magX);
  22. title('Discrete-time Fourier Transform in Magnitude Part');
  23. xlabel('w in pi units');ylabel('Magnitude of X');
  24. subplot(2,1,2);
  25. plot(w/pi,angX);
  26. title('Discrete-time Fourier Transform in Phase Part');
  27. xlabel('w in pi units');ylabel('Phase of X ');
  28. %DFT in N = 4
  29. k = 0:N-1;
  30. Xk = dft(x,N);
  31. magXk = abs(Xk);
  32. phaXk = angle(Xk) * 180/pi; %angle 。
  33. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  34. figure
  35. subplot(2,1,1);
  36. stem(k/pi,magXk);
  37. title('DFT Magnitude of x(n) when N = 4');
  38. xlabel('k');ylabel('Magnitude Part');
  39. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  40. hold on
  41. plot(w/pi,magX,'g');
  42. hold off
  43. subplot(2,1,2);
  44. stem(k/pi,phaXk);
  45. title('DFT Phase of x(n) when N = 4');
  46. xlabel('k');ylabel('Phase Part');
  47. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  48. hold on
  49. plot(w/pi,angX,'g');
  50. hold off
  51. % Zero padding into N = 8 and extended cycle
  52. N = 8;
  53. x = [1,1,1,1,zeros(1,4)];
  54. %DFT in N = 8
  55. k = 0:N-1;
  56. Xk = dft(x,N);
  57. magXk = abs(Xk);
  58. phaXk = angle(Xk) * 180/pi; %angle 。
  59. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  60. figure
  61. subplot(2,1,1);
  62. stem(k/pi,magXk);
  63. title('DFT Magnitude of x(n) when N = 8');
  64. xlabel('k');ylabel('Magnitude Part');
  65. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  66. hold on
  67. plot(w/pi,magX,'g');
  68. hold off
  69. subplot(2,1,2);
  70. stem(k/pi,phaXk);
  71. title('DFT Phase of x(n) when N = 8');
  72. xlabel('k');ylabel('Phase Part');
  73. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  74. hold on
  75. plot(w/pi,angX,'g');
  76. hold off
  77. % Zero padding into N = 16 and extended cycle
  78. N = 16;
  79. x = [1,1,1,1,zeros(1,12)];
  80. %DFT in N = 16
  81. k = 0:N-1;
  82. Xk = dft(x,N);
  83. magXk = abs(Xk);
  84. phaXk = angle(Xk) * 180/pi; %angle 。
  85. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  86. figure
  87. subplot(2,1,1);
  88. stem(k/pi,magXk);
  89. title('DFT Magnitude of x(n) when N = 16');
  90. xlabel('k');ylabel('Magnitude Part');
  91. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  92. hold on
  93. plot(w/pi,magX,'g');
  94. hold off
  95. subplot(2,1,2);
  96. stem(k/pi,phaXk);
  97. title('DFT Phase of x(n) when N = 16');
  98. xlabel('k');ylabel('Phase Part');
  99. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  100. hold on
  101. plot(w/pi,angX,'g');
  102. hold off
  103. % Zero padding into N = 128 and extended cycle
  104. N = 128;
  105. x = [1,1,1,1,zeros(1,124)];
  106. %DFT in N = 128
  107. k = 0:N-1;
  108. Xk = dft(x,N);
  109. magXk = abs(Xk);
  110. phaXk = angle(Xk) * 180/pi; %angle 。
  111. k = 2*pi*k/N; % k in DFT is equal to 2*pi*k/N in DTFT
  112. figure
  113. subplot(2,1,1);
  114. stem(k/pi,magXk);
  115. title('DFT Magnitude of x(n) when N = 128');
  116. xlabel('k');ylabel('Magnitude Part');
  117. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  118. hold on
  119. plot(w/pi,magX,'g');
  120. hold off
  121. subplot(2,1,2);
  122. stem(k/pi,phaXk);
  123. title('DFT Phase of x(n) when N = 128');
  124. xlabel('k');ylabel('Phase Part');
  125. % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  126. hold on
  127. plot(w/pi,angX,'g');
  128. hold off

最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。

补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。

但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。

下篇博文我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。

 

 

 

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/83476742

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。