找回密码
 注册
关于网站域名变更的通知
查看: 688|回复: 1
打印 上一主题 下一主题

MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-12-6 09:48 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)" L( U" _; w) K9 _  }- t

+ M  w: J+ M/ u) }* ~

1 S% ^2 b5 U9 }3 F4 W0 `4 |这篇本来是和上篇一起写的:MATLAB ------- 看看离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间是什么关系
+ Y; X+ h% @6 R# R- S
& [" K  P- @1 o9 E但是这篇我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇之前还是先看看上篇。
3 q! @- l8 |* _
9 \6 \- U- I# [7 X2 U1 @这里默认你已经看了上篇。
1 l$ K3 v4 a# M/ ^9 W' |& q. K0 B. n
本文的讨论建立在一个案例的基础上:
; L6 Z8 Y* J! b' @! Q% N: T3 x
- A- L# X* e8 a; s4 \设x(n)是4点序列为:
- s" Q1 f' {" q2 h& d1 o, b0 V( F  f# Y
" w  Y3 ~) e. f1 V1 f$ T1 x& x

/ v" Z+ H, ]( l  m9 ?  r  |/ ~9 H计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)
6 t  k# W% a: T* a5 z3 b: H* B; L1 Y* a

( n  q$ x2 o- }我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.9 X- m& ~% M! a/ R) a
8 v, N/ {7 p; G, H2 u; T/ q
DFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N.
: t- u9 A7 Y& k6 z+ o6 ]
- j: q3 J4 G8 J9 zx(n)的波形图为:5 T/ O2 w( k  K

; Z8 n0 S2 M% X7 f3 k& `6 x& q& H
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • stem(n,x);
  • title('Discrete sequence');
  • xlabel('n');ylabel('x(n)');
  • xlim([0,5]);ylim([-0.2,1.2]);
    : a. t5 X1 B# [5 B, m. q) b0 _2 n. e
  
* s& _. b3 t- `" p: g  Y  l
/ T+ I( m- I+ z% f* }8 E
. x* n4 Q. g4 x+ S& j! M* I6 Q5 Y* ?
* L; |- e0 f- A. {
我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:
  h; `# [" L9 T3 z2 i3 [6 G4 T: Y( Y% C; h1 L
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • stem(n,x);
  • title('Discrete sequence');
  • xlabel('n');ylabel('x(n)');
  • xlim([0,5]);ylim([-0.2,1.2]);
  • %Discrete-time Fourier Transform
  • K = 500;
  • k = 0:1:K;
  • w = 2*pi*k/K; %plot DTFT in [0,2pi];
  • X = x*exp(-j*n'*w);
  • % w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
  • % X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
  • magX = abs(X);
  • angX = angle(X)*180/pi;
  • figure
  • subplot(2,1,1);
  • plot(w/pi,magX);
  • title('Discrete-time Fourier Transform in Magnitude Part');
  • xlabel('w in pi units');ylabel('Magnitude of X');
  • subplot(2,1,2);
  • plot(w/pi,angX);
  • title('Discrete-time Fourier Transform in Phase Part');
  • xlabel('w in pi units');ylabel('Phase of X ');
    5 `& Y1 w5 S9 x& k- K6 e1 i
   
$ c/ w7 L7 L) n: l2 u' k& U, V
! ~  N+ V0 W+ f/ D7 J5 X

) ?# u0 e- }- L/ F/ N! N+ V4 Z
: u3 O' G9 S. g- ~6 t  U% o1 t6 h  n; r8 Y8 a0 w' _+ A8 o$ o
% J; _* a! E/ s3 w/ }8 n) H& Q
当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
% R+ Y% i' P/ d* ]0 S5 i$ O9 l) A. b  L
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • %Discrete-time Fourier Transform
  • K = 500;
  • k = 0:1:K;
  • w = 2*pi*k/K; %plot DTFT in [0,2pi];
  • X = x*exp(-j*n'*w);
  • magX = abs(X);
  • angX = angle(X)*180/pi;
  • %DFT in N = 4
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 4');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 4');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
    : d; Y" n  o1 ]: Y- v& [5 K9 K
            
0 g. M. h% ?7 Q" t8 }1 j
4 U% g( L: Q6 m5 u& t/ Y! |
* D( ]& ^% _3 ^; K
1 d# J/ ~! Y9 b5 `- \4 K/ W
可见,DFT是DTFT上的等间隔采样点。
% T) F1 n. |. ?+ Z3 E8 H
# Z- p, V/ o2 W2 M5 V3 l6 r2 `* w  g6 p& ~4 z3 v3 z+ z
当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
: L% p) `" p' e. F
7 ?/ M6 U1 V) E- [" j% p
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • %Discrete-time Fourier Transform
  • K = 500;
  • k = 0:1:K;
  • w = 2*pi*k/K; %plot DTFT in [0,2pi];
  • X = x*exp(-j*n'*w);
  • magX = abs(X);
  • angX = angle(X)*180/pi;
  • % Zero padding into N = 8 and extended cycle
  • N = 8;
  • x = [1,1,1,1,zeros(1,4)];
  • %DFT in N = 8
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 8');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 8');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
    ( s% C/ v6 k( h2 X0 L" A: h, J
               L! ^' D# v9 ?

, R7 E: e. z" N% r4 _1 g* Q
& y8 U  ^% w: p* {+ }
" m- t# Z9 c$ V$ ~6 E. v1 S* o
相比于N =4,N =8采样点数更密集了。
2 @* l! W" b3 q9 ]$ Y3 k# M  e
8 d: H7 o' M% h' u当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:. ~) I1 O1 Y) I( J

2 G, w3 a  X- F6 n2 \+ {$ e5 Q) @) O
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • %Discrete-time Fourier Transform
  • K = 500;
  • k = 0:1:K;
  • w = 2*pi*k/K; %plot DTFT in [0,2pi];
  • X = x*exp(-j*n'*w);
  • magX = abs(X);
  • angX = angle(X)*180/pi;
  • % Zero padding into N = 16 and extended cycle
  • N = 16;
  • x = [1,1,1,1,zeros(1,12)];
  • %DFT in N = 16
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 16');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 16');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
    ( T  A) ]! ~( W5 s- K
             0 H6 j/ R1 F& r# q8 d3 a* t

4 _% `6 }2 J/ T9 r! B8 R
4 t* L0 w2 C$ X; p, L

1 |4 ]: p1 g  p/ P! C+ hN = 128的情况:; ^0 f) R6 U/ n7 m8 S4 \& V# T

1 Q/ ~. e* S8 k
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • %Discrete-time Fourier Transform
  • K = 500;
  • k = 0:1:K;
  • w = 2*pi*k/K; %plot DTFT in [0,2pi];
  • X = x*exp(-j*n'*w);
  • magX = abs(X);
  • angX = angle(X)*180/pi;
  • % Zero padding into N = 128 and extended cycle
  • N = 128;
  • x = [1,1,1,1,zeros(1,124)];
  • %DFT in N = 128
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 128');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 128');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
  • 9 `6 [6 x# l( L
            
" x0 A  J; l6 X: y; q) M2 ]

6 f: d( D. {8 _! r1 C+ v5 \5 ?; |( H: |8 w& m  \1 U7 t  `
可见,随着周期N 的增大,采样点越密集。! @$ Q2 e. J  A# `3 C
' Q* K/ B5 }* K; l
  i& I0 n* T' X: V
上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:! U) {4 x+ b5 Z2 h* n, S" @

5 y7 t+ F& W& W" y$ {
  • clc;clear;close all;
  • % x(n)
  • N = 4;
  • n = 0:N-1;
  • x = [1,1,1,1];
  • stem(n,x);
  • title('Discrete sequence');
  • xlabel('n');ylabel('x(n)');
  • xlim([0,5]);ylim([-0.2,1.2]);
  • %Discrete-time Fourier Transform
  • K = 500;
  • k = 0:1:K;
  • w = 2*pi*k/K; %plot DTFT in [0,2pi];
  • X = x*exp(-j*n'*w);
  • % w = [-fliplr(w),w(2:K+1)];   %plot DTFT in [-pi,pi]
  • % X = [fliplr(X),X(2:K+1)];    %plot DTFT in [-pi,pi]
  • magX = abs(X);
  • angX = angle(X)*180/pi;
  • figure
  • subplot(2,1,1);
  • plot(w/pi,magX);
  • title('Discrete-time Fourier Transform in Magnitude Part');
  • xlabel('w in pi units');ylabel('Magnitude of X');
  • subplot(2,1,2);
  • plot(w/pi,angX);
  • title('Discrete-time Fourier Transform in Phase Part');
  • xlabel('w in pi units');ylabel('Phase of X ');
  • %DFT in N = 4
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 4');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 4');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
  • % Zero padding into N = 8 and extended cycle
  • N = 8;
  • x = [1,1,1,1,zeros(1,4)];
  • %DFT in N = 8
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 8');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 8');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
  • % Zero padding into N = 16 and extended cycle
  • N = 16;
  • x = [1,1,1,1,zeros(1,12)];
  • %DFT in N = 16
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 16');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 16');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off
  • % Zero padding into N = 128 and extended cycle
  • N = 128;
  • x = [1,1,1,1,zeros(1,124)];
  • %DFT in N = 128
  • k = 0:N-1;
  • Xk = dft(x,N);
  • magXk = abs(Xk);
  • phaXk = angle(Xk) * 180/pi;   %angle 。
  • k = 2*pi*k/N;          % k in DFT is equal to 2*pi*k/N in DTFT
  • figure
  • subplot(2,1,1);
  • stem(k/pi,magXk);
  • title('DFT Magnitude of x(n) when N = 128');
  • xlabel('k');ylabel('Magnitude Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,magX,'g');
  • hold off
  • subplot(2,1,2);
  • stem(k/pi,phaXk);
  • title('DFT Phase of x(n) when N = 128');
  • xlabel('k');ylabel('Phase Part');
  • % Auxiliary mapping, highlighting the relationship between DFT and DTFT
  • hold on
  • plot(w/pi,angX,'g');
  • hold off, z, d8 l& Y6 X3 r3 H2 _
                                3 \# Y; |% p, K) F" m2 n; [

7 F" v7 \% ?, i- W( N4 w最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。; G4 P' i' l7 m: P" r: w7 l

' i7 H0 S5 v9 s$ y1 e5 l6 P补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。
  y7 b: ~) K9 t8 e. a* N
7 c4 b, `5 }+ I( \. b1 ~1 k" w- |但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。
7 m) N3 E6 s8 L3 L2 g! z5 U+ Q1 F' J& N8 S7 f
下篇我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。0 s9 S7 Q1 ~# m! k9 W" B. o, _
# ]7 e' P' C2 f+ y- z9 E7 f/ W
: n0 |* d8 v( [8 T& F

+ |. f* a8 D: r: ^/ C1 q

该用户从未签到

2#
发表于 2019-12-6 17:54 | 只看该作者
小弟要好好像大哥学习呀
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

推荐内容上一条 /1 下一条

EDA365公众号

关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

GMT+8, 2025-11-23 22:00 , Processed in 0.156250 second(s), 26 queries , Gzip On.

深圳市墨知创新科技有限公司

地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

快速回复 返回顶部 返回列表