|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
MATLAB ------- 用 MATLAB 作图讨论有限长序列的 N 点 DFT(含MATLAB脚本)7 q8 l* V/ o; `
7 p0 g$ f9 @' \5 a* B5 {: _
" Q2 F0 \% R9 j+ I9 p; d这篇本来是和上篇一起写的:MATLAB ------- 看看离散傅里叶级数(DFS)与DFT、DTFT及 z变换之间是什么关系
9 D/ p# A1 w. \$ u
/ n$ ?- t& e/ Z但是这篇我最初设计的是使用MATLAB脚本和图像来讨论的,而上篇全是公式,因此,还是单独成立了一篇,但是我还是希望看这篇之前还是先看看上篇。
* ]& `; u$ x, M6 }$ A3 @9 M
" O5 {$ x& r f这里默认你已经看了上篇。% z8 v! h8 P+ m _; a! ~. D( Y0 a
+ e/ k, P5 s; ^, M本文的讨论建立在一个案例的基础上:5 k9 q' r3 C4 [
3 M! E! d$ V$ n7 F$ u% T) z5 E" R设x(n)是4点序列为:; _+ f* J7 P v8 M7 D7 J. w/ @
! S8 n: X1 B; _$ E% n) ^/ V* {4 ~
6 P. R5 C+ V! `- q+ h; [. h" o5 U
! Z6 b6 v# w; `/ L4 T计算x(n)的4点DFT(N =4),以及(N = 8,N = 16, N = 128)
# I* Q0 [. k5 H `( E5 p0 g1 d& V. i& Y7 @7 m, g
+ I8 _0 ^. z# i$ ~; n
我们知道DFS是在DTFT上的频域采样,采样间隔为 2*pi / N.
) }" p* B& D, f3 Z0 D6 i
2 q% D' u- j! u. f+ vDFT 是一个周期的DFS,因此DFT也是在DTFT上的频域采样,采样间隔同样为 2 * pi / N.
, {/ |0 M; ^9 p4 z, w+ y1 `# Y6 z9 w& ^7 e( p0 \
x(n)的波形图为:, p- |( F+ H; G( \
6 Q4 B" w$ 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]);
, \1 G8 H8 V1 X7 b! J3 X% U4 I # V8 b& y6 s- v0 B. `
; V: y [. s- L5 G+ J, s. ~
3 v) t" f! d2 I2 Q ~+ k( a
9 \% y0 |( P! E6 c. V+ O3 s我们先作出 x(n) 的 DTFT,也即是离散时间傅里叶变换,给出脚本和图像:( Z$ _$ d9 J* {3 t; r
" V3 X* M1 F6 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]);
- %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 ');
8 S9 ^8 w1 d/ M: t! T) E ) V2 y8 E/ _. G. x
, X+ g5 V" i8 {, q
# t1 L! _% t, n
1 }4 F8 k, S; N# f; V/ p; `
- g* S; I! W ^ z; c2 W
) ~# K1 J( w& u) d/ S( R4 S当N = 4时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:
8 C) l. B# f% t# u- P1 @. N* E& I7 F$ x% ^" { L! P z$ 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- G# j3 @( j$ ~( W/ p
! f, k* z: M& C$ [3 D) _6 w1 M K% c; r0 G$ l
. Y3 z# J, @& T3 X4 B# V$ d3 [* F. K. E- R5 {7 S: E3 {$ t
可见,DFT是DTFT上的等间隔采样点。2 @3 K0 m$ X9 @6 }/ [/ q/ B$ L
* ~% W2 x' J5 ?' i( A% Q5 G& h% k
7 r5 x* X( z% |% C/ B. z当N = 8时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上:) Q2 j9 G n+ Y; v
8 E! L$ o9 }" \" 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
. Z7 X5 k# A% h: b: [
6 k+ k: f0 ~7 ^9 p) B, @8 w
5 D" u3 c7 `+ [
3 @: I) |& @& H- R
8 [, E) o# i! v' B1 j' D0 h
相比于N =4,N =8采样点数更密集了。! r; D% n- D$ [# W8 c
8 i/ v7 T* o9 H* E
当N = 16时候的DFT,为了显示出DFT和DTFT之间的关系,我们把DTFT图形和DFT画到了同一张图上: `: V# x1 b, \ X. Z6 x9 K4 M
" X% @* [' J$ e' h8 ?4 J& e5 s7 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 = 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
5 `9 D" U( L0 X% d& m ! l! \; p0 C, e8 f# [: F
1 R: ]* {9 h4 V$ e+ h# Q9 o
* Q5 C; t% {& s4 Z
7 V+ G# k$ r# r0 p% M' n2 K6 I
N = 128的情况:" b9 H) O6 i+ d* X
. Z8 O' {) r o6 @7 u* g9 K* n
- 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 O9 [8 N" \4 o+ y, y
3 w2 v/ X- E, Y6 K/ F/ x$ e: j% E
6 M2 U3 Y' Y( R5 t$ j9 c
! c& O! C( W$ Z
可见,随着周期N 的增大,采样点越密集。+ v. H. y2 d0 Z' F5 E3 X7 o2 z
* v1 {! t) z- \- C7 K! u2 l
# p4 o& T5 ]+ y& S5 G
上面的程序都是我在我写的一个大程序中抽取出来的,最后给出所有程序的一个集合,也就是我最初写的一个程序:* R7 l$ ~( e. d9 _
9 H- j# x! V3 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 ');
- %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+ w* M$ U/ g1 W# U' x6 ?
1 R K* X& L; E+ w8 v' U
" H, A% U$ ?6 _2 _* d
最后需要说明的是上面通过补零的方式来增大最初给出的有限长序列的周期,这样只会更加DFT在DTFT上的采样间隔,也就是频率分辨率。6 y# ~7 [( w! K8 `1 I( J+ L4 H
( @' h) o5 v" E: s) H! R补零给出了一种高密度的谱并对画图提供了一种更好的展现形式。
9 n& d* f6 x. L; a
5 o6 c. M$ `" g0 U, P: y但是它并没有给出一个高分辨率的谱,因为没有任何新的信息附加到这个信号上;而仅是在数据中添加了额外的零值。
; r& E8 k, I& G+ Y+ `" e- J" z( y
, d& d, A0 \' T* X8 G0 }& Q% A下篇我们继续说明,如何才能得到高分辨率的谱,我们的方法是从实验中或观察中获得更多的数据。( P- A3 W! U; ^5 U1 [2 s9 Z
: E: F; S9 \3 n! h( {% K
1 _8 X- H$ F. {
5 s/ h% W J/ t' p; A+ y3 F |
|