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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)
3 l- e1 b2 V9 H: a1 K5 X4 Q
6 A  {0 `: i1 T1 A

2 S! x( S+ b- ~  I5 g0 C0 U6 b这篇本来是和上篇一起写的:MATLAB ------- 看看离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间是什么关系8 j: q6 \* H. \9 V

* ]9 i9 D  {# }; y. k$ y% w$ w- o但是这篇我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇之前还是先看看上篇。
* [( ^2 w4 p% w# l- c7 x$ {6 E  N% B0 `' l& P
这里默认你已经看了上篇。
( A* d, d# j# C8 B4 F
* ~4 N  i. f2 i% L; K本文的讨论建立在一个案例的基础上:2 B4 i1 u  K5 k# A' |1 X' g& m& u

: i+ O% @" r, k1 m1 O, M1 U9 {设x(n)是4点序列为:
/ Z7 G+ O. x0 {6 }
0 u7 H8 k* y0 P* T& Y1 F( H3 F
* k/ }0 _0 h* t6 p5 k% n

/ a; M" k" |% V8 b/ @4 [" h' E计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)
" N# \1 M7 t2 U9 {6 l( Q
) g- K" X  M, s4 X# W0 y
  }, o9 q; h4 ~' e6 \9 J5 C7 E我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.* l% R& D/ g: _% _) z9 R
+ |8 i' x1 p( _; J: f! i
DFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N." l& z+ i  M! G. N# O

& W  {+ Q5 F% G1 n1 Y# p" X7 Zx(n)的波形图为:
, j* E& R' e4 F. C) ]# j" B
* Q& t3 I4 O: o. V
  • 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]);( G9 Y/ {+ g- J
  
* M6 h+ L* U( g  z

% ^$ }3 t. c5 I8 T

9 O" r0 b# h+ g) C
8 I3 x2 F1 W3 {4 V1 n* W我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:
9 a7 V: z( y& p; Q
' K+ Q7 w# ^: A% G+ g
  • 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 ');
    6 C) A' G, e6 L. \% x
   
& |$ k( t* N  e. O: g( v( m
" \6 U3 x3 r0 t* |; F. {' n: R

8 B! P" G* t- u% i) H0 @
/ Q- c& E3 f+ I/ y/ t) ?1 p2 v4 S, m4 X2 r

5 O' y; |$ ]" Z) }$ m) ?  f当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:% |$ q" D6 S, s% F

. m+ j8 t% U8 x9 B
  • 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
    % c2 S2 Q5 j* ]* z
              I: g, y7 N6 q6 R% y

+ l7 K2 b8 N  R* @5 q7 X; s
8 J* g2 F0 }' @& C4 D3 n8 |2 V
$ l* R7 z; r% T' q
可见,DFT是DTFT上的等间隔采样点。" n7 t8 ^8 J( D7 R0 q

, Q6 m/ R" {2 [6 b2 Y8 O
* P+ y+ Z' N4 P# {% {0 z当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
  B/ C* ^- Z' A( D
' f: f7 e% {7 C* z2 N+ \. 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( n5 k$ m7 K' O2 }4 E: r
             ; X9 K" ?. R- D/ n$ U$ u* W  n6 m
- d! O% z2 [+ ^! D

+ l5 Z+ |  x/ p! M6 L) Z5 t6 O: ^7 f4 `: z$ D
相比于N =4,N =8采样点数更密集了。3 D& N3 S7 E# Z) C) M

. ?! e" c, i# q) u9 y6 i当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
# k9 C& Z$ D# F2 M0 L1 F" T* L. b& t, h# n6 ?5 Z% C  F4 S$ D
  • 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
    ; K' y2 @. f6 H5 s0 l
            
7 B4 P9 z3 a* z3 a; b
; l5 J$ n' f3 Z& M( g
1 E1 d' c: k( \* k# ~1 j4 k

1 `8 C* K' S8 a$ o. r) d! |N = 128的情况:
* C# E  q+ V# p9 k7 w% t" ^+ ?, \9 t/ O, M2 C5 ]
  • 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

  • 4 f1 f% I, T; b5 f: g3 R
            
2 L7 F3 R1 x. r" x7 k1 U

' b; K$ j' x' o  L
/ |. O$ `' {. H; z可见,随着周期N 的增大,采样点越密集。5 `7 d% g/ {3 u* ?5 X/ W3 L
$ g. ~" d2 {+ f% [: m, V9 \

* K+ A' `5 {! W1 U' \/ i上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:
8 S# Y( v  ]' [, U
  x1 X( c7 b9 C: |- x
  • 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% g/ P8 ?! {. A
                                
8 f! o' x3 f' P. m% W$ i# s. Q" k ( s' d+ L/ k9 |; J$ h6 g4 L
最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。! l8 n# ~; p$ H& E9 V2 `  T

9 }% s, }- b; Q, F& t补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。2 Z0 ?5 Q2 [' f' a. c3 g! V
" K& |2 [6 @3 a1 R2 g% O
但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。
: L3 S3 d: d8 s4 v* A2 l) j' ?" O: `3 A6 I9 ]- d: q& I$ m7 F3 o
下篇我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。
) B+ {: L/ `5 C: `2 b4 [) u4 z" P9 @" j" a% P) R1 P
6 i* X% N8 f: `7 c' d) O; e4 z

- k, T0 G. p4 e$ }; u; P

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

GMT+8, 2025-10-5 12:14 , Processed in 0.156250 second(s), 26 queries , Gzip On.

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

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

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