|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)7 \9 J; Q' y5 m% D/ b" n3 Z
D/ I |: J3 t7 E# @9 G4 M" {0 c* o: u+ U! B9 l
这篇本来是和上篇一起写的:MATLAB ------- 看看离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间是什么关系
) [2 Z: i9 I+ J4 X9 o& y, `
- _1 I* I$ f3 Y8 O4 u但是这篇我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇之前还是先看看上篇。, O+ v v8 [! ]) g* y+ l" P4 Y0 _
}( f+ K$ p, J6 W
这里默认你已经看了上篇。
- e" t2 J! t6 \- B# v3 ^% W% x. f
本文的讨论建立在一个案例的基础上:
; }1 N& {$ c" n- k1 m2 {
. D9 C, M" E( }6 c设x(n)是4点序列为:
# l& k1 O- ^6 S& s) p# f* u* p6 e1 {: u' y: M# c- M) j2 b
% d% K( d2 s2 V" |
7 y. T/ L4 }0 B+ Z0 P计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)' K/ N2 H* S Y( C& L! D. ^9 p( N, _
7 {5 P$ G% q4 m/ s% C$ c; _3 S
3 ~) e+ O& Y. S8 E我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.3 i5 |8 m2 S' U# [) t* U" ?: W
- o& u: Y( m; W5 t2 t9 j; a, V
DFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N.
\3 L5 X! X1 W' t7 R
; D7 b( {8 i( b" U& px(n)的波形图为:
s$ e/ S9 @ F; `: ~6 v6 r- J6 S+ K' q, l5 y& ~ M
- 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]);
" x4 i: {$ ~5 S+ V! Q+ X " c: @+ A! O+ l E
: l! m$ m B! S u5 R0 p+ X/ ~2 D
4 |7 X( u' j. o7 V% I+ C& U2 B
: P; x% F# s/ K' h/ D# q9 h! L1 M
我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:
; h% _* S; @. d' ?% u
: [' U* |8 g2 T: s' d" P: Q, E, 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]);
- %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 ');
/ k& C' H# N3 w4 B
3 J. p6 C5 ~6 Q" G7 g1 E/ X3 [
# Y2 p, ~1 I, S3 N+ n
9 b5 V/ K/ K ?) m9 Z: A
9 E* x0 g: ^, q v
$ l n. O- H% @% D; ~% |
3 H/ H* a: ^( F# W$ ]" h当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
$ T- ^- w% k( d* f$ S7 Y% Q8 Q( \- R3 m; v9 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;
- %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
# l( U2 C# V9 M& I& s( e" Z
( ^+ }; Y( K; I5 K" T" q D
6 B- L: V2 p, u3 j! p: Y. }* ^
7 Y/ H8 |& I) g" a
; G. g; A; l3 }2 S
可见,DFT是DTFT上的等间隔采样点。" p: i+ L/ r4 a2 Z1 ~+ b
( r: v, W/ ]6 O& z1 m% m$ H* ]( T
- W* @( h& e; g; Y当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
) t3 _% _" W6 J% C7 @; ^' Y7 _- Z3 A/ P8 Y( t" u
- 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% c0 K+ j" O2 S1 B
; T$ h/ t, W( o5 _
5 T; C% w* z2 c& o
9 ~ l( @, L8 Y/ I" t5 r1 `, q9 A
1 Z' X% O6 x2 N: O; d# R) u6 [相比于N =4,N =8采样点数更密集了。* g6 N1 J" O8 B: f" b, ?
& E$ G' \9 _5 v. t* t当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
" \( h1 g% A' ?0 }9 t F8 p) Q1 L* Z9 l* {& j) y
- 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: y, Q5 }1 |3 e! X% s9 Z
1 [1 l. t+ }4 ?% R' D
' S/ u. J: n/ H" j. V& |
; k! C/ L9 E0 C: z, X7 A) p# R# x) s) E9 ?7 A% w
N = 128的情况:
# s& ?2 N+ \3 }* T+ T7 I, O! l1 j0 ?( o. W% n0 c. 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;
- % 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
- e8 r1 y# N/ b& Z g9 E C* g; v7 d * A" P$ j ?5 z" W
J; k4 r/ L8 i/ @( m& j7 o! Q$ ], |$ e: X
可见,随着周期N 的增大,采样点越密集。5 K/ b. [) ^$ X! H9 C
4 A: v- d# V! ^3 f8 t, D+ l5 y# e& f) K2 L& w/ d: u# n
上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:8 U, Q( I! s/ b- ^# |1 x
0 B4 k& g1 @& ^4 E- 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
N: c ~3 I+ i' i# D. ?! `- T# o) t
' Q9 e; g8 D1 \5 @' t4 ~: `+ m
2 Y# ]6 J5 _ B! o最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。
0 A: a, Q) e0 L9 n: D% ]! l$ P0 m+ l1 H5 p4 H
补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。
$ h; x: g5 ~* b
! {. Y8 t7 ?. F/ y: l( d. \' r# X+ _但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。
( ?1 R/ E0 J2 d2 j" Y; |+ s9 G4 A9 Y: O- D$ h% A9 `9 ~! f
下篇我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。9 v* o. Q! i1 X+ C" G: z
; S: w- a. u* W a8 y: r5 h
# r# F5 J3 W# x N% I( I
! {/ R+ I0 |0 `' [$ j. a
|
|