|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)$ w* }5 y% k' c: B u( k- ^9 c2 }
" {$ e% @( Z& \: `2 T5 y
% @7 B5 {, Z$ g% A$ s这篇本来是和上篇一起写的:MATLAB ------- 看看离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间是什么关系9 b& f) ?8 |2 ?9 e. k
7 R9 D/ u) A) |& ?
但是这篇我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇之前还是先看看上篇。: ?/ D5 J$ ^0 D- D
' O# v4 k# e9 X这里默认你已经看了上篇。
! u# M3 p; d1 K; \- F3 s c: Z/ `$ V$ q- k+ L. u% n! O' C+ j
本文的讨论建立在一个案例的基础上:
/ V4 Q: E3 m: g; M) ]7 X$ e1 z
, o3 ]$ S/ _# I设x(n)是4点序列为:
: X/ Y4 j1 ~1 _9 |
% I `2 E( [, c$ h
7 h1 m( f7 t/ L$ v: N+ F3 O9 l8 L E2 Z
计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)
" C" w9 D% h) U: L P3 b" z: |6 O# V9 _) F' b
& W8 c4 P6 @5 Y, a% Z
我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.
$ Q, W+ p- y' o; |( m/ H/ M4 ~& @& `7 k9 L" K2 g2 l) e0 ~5 A
DFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N.
+ l9 u+ I M% }- r- O
0 p# H: y! I2 T4 h7 m r5 Px(n)的波形图为:7 C6 o' z# m$ I
7 U5 R! w! V: D* k$ s5 h$ z- 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]);# j4 f4 U+ [, [( z& D0 ~1 @* r
% S3 X! b6 a: {. A8 l
' P% Y N& ^. Z! F
6 T# y7 ^' F4 E& M4 i M
7 I# t; p- |, k
我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:
/ @2 B2 h- o5 k4 k% p( x, z% T, T2 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 ');3 {9 Z( ~5 s$ [
# j0 k% f+ G. h8 `4 N [" i
' t( [7 B+ y7 ^+ p. |* p1 B$ @
; a8 k/ A3 \8 F8 C" H# U
6 m1 i4 w8 `- w' X7 O
; F4 N1 V# r6 @6 ~/ k% I1 U: X% F& e5 Q
当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
6 F9 w/ w0 x# d5 b" w- V: n9 N$ Z! s
$ z0 l S6 t) w9 a; ?6 B1 B3 A! O 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
7 k2 T5 M/ _6 t( r+ g
. c$ m: ?6 R t& f9 z: ~2 P
! R( _# z5 ~* l! z
$ n* K) t) t# X6 r" A! l z% y, f
% }) b, Q7 q! }1 j
可见,DFT是DTFT上的等间隔采样点。
' K9 [4 M& _9 e5 g' k$ g
t9 V$ l: [' V4 I. B( O9 O4 Z9 O- p6 ?2 v8 C7 E
当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
/ s/ A7 L, O2 [! C: o" }3 H/ p2 E: p5 H0 n. Y: r
- 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
2 e9 ^+ ?7 H2 @( n. P7 A9 _7 r * o' p6 I" L( c7 s
! X* r! X! J% V5 U1 _+ T4 o
% z2 w% I' `# |+ m1 N* {- i' ]( p3 J$ K2 M& g' f) e2 @
相比于N =4,N =8采样点数更密集了。% T& |) p. J6 v$ b/ g
! f2 E4 T; ~( ^当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:; {8 y! s% ~& O
( h3 l8 |+ _, }) _1 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 = 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
. D9 F# w8 L9 ~! E: Q' d* L! R) Q$ y 4 P8 N2 ` E Z# J2 w
5 E+ `& A- M* T; ?- c/ k
! D E9 G, V% F+ ]' E4 @) ?/ ?% i
$ p4 k q W/ a Z2 ^- q2 g2 ~
N = 128的情况:" }8 R) Z+ Y5 n$ Z
. j$ M8 E/ @2 n# f+ g& k: u, }7 ]5 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 = 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
# v& D$ z4 ?' W3 T f
k$ s7 J4 ]6 K6 l8 D" }
: Y8 Z! k2 C) K! D- J; V
y! H! v$ ]* h6 r/ Y1 p9 P6 M可见,随着周期N 的增大,采样点越密集。' y5 C- f1 e/ V3 ?2 }& p
; A$ c( D# k. O+ Z- K
- ?5 |9 v# E" H. D8 A' W7 n! t
上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:, x/ X; D# N5 `, V7 c
& z! c" [9 k- B* f# I
- 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
. D! T" Y* r) g$ a5 b 9 ~& u$ R4 _7 S7 {, m0 C
0 M- ~- f1 O' F( o最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。
) e& B) ~2 Y9 Q8 M+ Z0 L5 ?# L3 u! ]* i l9 @$ q4 o
补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。7 _( p8 C- I8 Z6 O8 L' K$ s, i( y
2 D- i; C1 g( M7 Y6 R9 @! W8 q但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。6 ?8 y2 P% P. u, d
- b: j1 @% g% B0 A3 b+ B下篇我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。; `2 Y9 {2 y8 k8 w% W; f. t
2 P% Z; }: N# X1 h6 Q, Y
$ b2 p0 u' `$ s; U4 R, e3 y9 f/ S
3 e+ u6 N; ]) O6 S: |- X |
|