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

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

[复制链接]

该用户从未签到

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

EDA365欢迎您登录!

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

x
MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)5 [& p! u8 c3 G1 b* D
; o" V+ A, x$ w4 m2 R

' s; [5 Z  b  ?+ @. w这篇本来是和上篇一起写的:MATLAB ------- 看看离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间是什么关系
" m5 S) r& o8 g4 K- G8 Y% [1 y2 @# J' o' H
但是这篇我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇之前还是先看看上篇。' m  i' B+ G1 U

; y! |; h' ]1 N/ q这里默认你已经看了上篇。! o8 q0 \# n4 t' W: |4 L) X
" ]8 H* u! m" ~: [
本文的讨论建立在一个案例的基础上:$ W1 q& g2 E2 B" n

; G1 H. o5 X, V/ ]' p设x(n)是4点序列为:" |" _, w5 T2 e3 y

6 M  b$ e1 u& ~6 r1 U1 q) e
. N  q# v: y0 K3 C. T
, R8 V8 B2 y7 v: s/ a2 @
计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)' Z2 _" o; J1 F: V: Q
! ~+ y; U4 R; t8 [* B

% P" C  c+ h6 z% s我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.
0 J# _( u' V4 n7 J0 C0 f. n6 |: K0 k% N* ], V( k
DFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N., K1 C" K5 D' h: N0 d

3 \" J0 R* j# X2 u' ?x(n)的波形图为:$ e3 @; f# N+ [* E$ q
0 ?! Y) k4 y, z3 m! t' O8 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]);- z3 C' T# s" A: d7 ]- t3 ?! l- T
  
8 ?$ }/ _) d( n4 z; f
+ B6 @. N  X  g

% _% d# Z' r/ D$ I! X- H8 w3 ^4 O$ I1 c3 Q4 r* ^
我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:/ U8 l2 Y0 D+ B9 ]( @2 A
& y& I$ @3 P; A- b" F
  • 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 ');
    ) U% j" z( k% }5 h3 y+ C9 {
   6 ^) [$ Y' m) J: a# S
( \7 T0 }0 Y* j0 @
- i. H! a+ \( {2 I2 U! T

- v* l( {2 @5 t
2 W3 w/ F) k/ R( f, H  b- I  W, q+ c) `9 s* G4 y* M: Z
当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
6 U/ W, M- H3 g8 A* ]
2 h9 @8 x- v8 O% e
  • 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$ c8 D. ]8 x5 V  @: F0 Q0 P( [* S
            
* Z/ k6 O  N' l8 w
7 j) a/ f  Z' c5 c: Z- x
7 u+ V7 j& p3 ]

/ V) v& z) r: S6 I6 p可见,DFT是DTFT上的等间隔采样点。
8 s$ n9 r% N! }3 T# P( p; z( M) Y" M" f; u6 C0 S

: G) g& l; ~' @% c当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
8 S5 y" H2 h! Q4 h
( v1 L1 _' z8 H! V% y) o* b9 j9 ^* v. [
  • 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
    5 ?7 Y5 R, }5 u4 P
            
/ O+ X" f# e; `' o7 f5 A
: Q1 Q$ ]1 F3 i2 E# H2 [0 j
+ @1 |' S. [# Y) z: c# c8 z2 e

8 a$ N; ?6 j! e( H相比于N =4,N =8采样点数更密集了。
. U3 B( [- B# P0 ]) V, {1 B+ b( T! [+ f, }8 V! }
当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:  u( V3 z2 k0 T# E# v* Y! N

2 Y/ D- z8 M! l1 L+ M
  • 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) b& n! D# E/ e8 c+ A" _
            
7 s, m# z) z/ \# ]( y
+ N) Y! Q% j, `' \* l7 C

* b1 h8 L  |6 G8 k7 R, q+ W; ^  ]1 \) l$ r
N = 128的情况:
) V+ v6 o5 H. e# R" }, Y8 r/ j  x3 L) p2 o& b8 f: {$ s
  • 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
  • 3 N; x6 ~* [, ?3 I' E! b
             5 R) B$ c% j- [2 b3 L8 a) h2 o

/ g) F% ]; \3 \8 M+ ^; D& m2 t% N! b+ F  }
可见,随着周期N 的增大,采样点越密集。# C. m4 u# G$ k: Q8 c% ^" {

% T. @$ \1 T; G5 {/ n# Z2 D9 i) b/ j; Q. u: `( y
上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:
, f- y- l# O* ^1 y1 i" m; [/ i% }0 R4 C4 {, Q
  • 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' e) e+ p, Z* U3 v
                                
2 t; `3 g9 x. B, g, P. S8 b
5 p. r& g4 B8 k# ]: l; d; S最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。6 a; _4 i5 ?5 S2 o8 S

1 z% J" {9 g1 S' v7 W补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。
) Z' c" k1 I2 t' L( Q
7 c/ i: r9 B" J9 l但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。
% ]2 `$ x. w) P9 d- Y6 L- f/ q4 v! I1 ~/ j5 s/ ^$ H& y
下篇我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。
6 C% Q: O: s2 `. N1 p3 z
7 R; o! h8 F# W% B% }
( t4 x2 ~6 [2 f, Y# {8 f

+ J, {( i: K; k) @

该用户从未签到

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

本版积分规则

关闭

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

EDA365公众号

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

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

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

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

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