|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
: \/ h A) e( A; }& i上篇:MATLAB —— 信号处理工具箱之fft的介绍和相关案例分析介绍了MATLAB信号处理工具箱中的信号变换 fft 并分析了一个案例,就是被噪声污染了的信号的频谱分析。2 n N6 q$ T3 D8 X& e4 m ?* |1 d
/ W/ H; H! E4 t+ E
这篇博文继续分析几个小案例:$ u3 _$ h& E/ _) e
; I" {: }3 p. {2 ?" \+ z+ n
Gaussian Pulse
" T% N8 \/ F, S& d; _2 i这个案例是将高斯脉冲从时域变换到频域,高斯脉冲的信息在下面的程序中都有注释:/ p3 Q* M0 i9 p9 ^- g
; v+ @* ?1 V& C
- clc
- clear
- close all
- % Convert a Gaussian pulse from the time domain to the frequency domain.
- %
- % Define signal parameters and a Gaussian pulse, X.
- Fs = 100; % Sampling frequency
- t = -0.5:1/Fs:0.5; % Time vector
- L = length(t); % Signal length
- X = 1/(4*sqrt(2*pi*0.01))*(exp(-t.^2/(2*0.01)));
- % Plot the pulse in the time domain.
- figure();
- plot(t,X)
- title('Gaussian Pulse in Time Domain')
- xlabel('Time (t)')
- ylabel('X(t)')
- % To use the fft function to convert the signal to the frequency domain,
- % first identify a new input length that is the next power of 2 from the original signal length.
- % This will pad the signal X with trailing zeros in order to improve the peRFormance of fft.
- n = 2^nextpow2(L);
- % Convert the Gaussian pulse to the frequency domain.
- %
- Y = fft(X,n);
- % Define the frequency domain and plot the unique frequencies.
- f = Fs*(0: (n/2))/n;
- P = abs(Y/n);
- figure();
- plot(f,P(1:n/2+1))
- title('Gaussian Pulse in Frequency Domain')
- xlabel('Frequency (f)')
- ylabel('|P(f)|')
- : f9 E3 U# K/ J& N( x
# Y9 f7 @. ^5 S. I, D 3 A9 e, k% H! y, Z+ S
高斯脉冲在时域的图像:
2 i- A0 O/ {& X! w5 S* i' ]- O5 ` z4 i' m7 n: v, }8 R, u
# c2 a* W( t) f6 z5 W' y0 Y/ |! v g& n) E5 S
高斯脉冲在频域的图像:
( I) V( h( y; @
+ }/ i) w k9 j
e* k9 k% r4 E$ _# c; v5 X0 u0 O5 m9 [6 `; L
/ l. W" L/ }8 C" O1 I$ K2 p5 {$ P! r$ u& S
Cosine Waves
& s/ ]* f7 C9 j; a* Z2 @. L
4 u3 i4 d3 q7 U7 T$ ~这个例子比较简单,就是不同频率的余弦波在时域以及频域的比较:! M5 V% c; P' R# L0 B
4 N3 f' G8 d% G. T
- clc
- clear
- close all
- % Compare cosine waves in the time domain and the frequency domain.
- %
- % Specify the parameters of a signal with a sampling frequency of 1kHz and a signal duration of 1 second.
: K4 u! s. `1 A; D- Fs = 1000; % Sampling frequency
- T = 1/Fs; % Sampling period
- L = 1000; % Length of signal
- t = (0: L-1)*T; % Time vector
- % Create a matrix where each row represents a cosine wave with scaled frequency.
- % The result, X, is a 3-by-1000 matrix. The first row has a wave frequency of 50,
- % the second row has a wave frequency of 150, and the third row has a wave frequency of 300.
/ r" H( w; B0 p# O9 A4 S5 z6 V) f- x1 = cos(2*pi*50*t); % First row wave
- x2 = cos(2*pi*150*t); % Second row wave
- x3 = cos(2*pi*300*t); % Third row wave
- - W3 y( p" Q& f G$ M. a/ @/ {6 Y+ T$ o6 T
- X = [x1; x2; x3];
- % Plot the first 100 entries from each row of X in a single figure in order and compare their frequencies.
- & S8 U5 |0 J( a* U, h! ?6 |
- figure();
- for i = 1:3
- subplot(3,1,i)
- plot(t(1:100),X(i,1:100))
- title(['Row ',num2str(i),' in the Time Domain'])
- end
+ M" q" w4 f) d2 R0 }$ s: e- % For algorithm performance purposes, fft allows you to pad the input with trailing zeros.
- % In this case, pad each row of X with zeros so that the length of each row is the next higher power of 2 from the current length.
- % Define the new length using the nextpow2 function.
- 5 U, ^) _2 c8 ~* }
- n = 2^nextpow2(L);
- % Specify the dim argument to use fft along the rows of X, that is, for each signal.
8 m8 P. f: a9 t- dim = 2;
- % Compute the Fourier transform of the signals.
5 J2 a& W: r* }5 i% c) g! x8 i/ ?+ a- Y = fft(X,n,dim);
- % Calculate the double-sided spectrum and single-sided spectrum of each signal.
# p" w5 K9 Q& @" S+ X2 a- P2 = abs(Y/L);
- P1 = P2(:,1:n/2+1);
- P1(:,2:end-1) = 2*P1(:,2:end-1);
- % In the frequency domain, plot the single-sided amplitude spectrum for each row in a single figure.
- ' P5 P+ a; C" P& W' {
- figure();
- for i=1:3
- subplot(3,1,i)
- plot(0: (Fs/n): (Fs/2-Fs/n),P1(i,1:n/2))
- title(['Row ',num2str(i),' in the Frequency Domain'])
- end1 b: U4 R! b1 e: d
# l, G6 R3 f# a+ {! Z3 x5 j8 K8 G
4 w7 X% k+ {% x! \6 X. o
下图是频率为50Hz,150Hz以及300Hz的余弦波在时域的图像:
( V; |4 C1 P8 p! X
0 H9 g0 _$ @9 ~3 Y" h+ n
9 h' a, A& o4 Q( ~) |9 |7 T1 R6 o* t2 q
下图分别为其fft:
# |7 @+ h* I. f" I9 G0 ]4 Y- A) W: Y+ V& f. ?
. E2 }: D3 Y' r7 \: R, v
, ?: `- Y% J7 P" z# c$ \0 e从频域图中可以清晰的看到它们的频率成分位于何处。4 ?/ ]# h8 r0 u4 S1 [) @
, B/ w y$ Q6 j% ]8 ^
4 Z0 P C0 ?: N) r% s! N5 z1 Y |
|