|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
3 H7 C5 c7 M v5 O @
上篇:MATLAB —— 信号处理工具箱之fft的介绍和相关案例分析介绍了MATLAB信号处理工具箱中的信号变换 fft 并分析了一个案例,就是被噪声污染了的信号的频谱分析。
, Z; i8 E- a, {6 R, _& L$ X! {7 ~# O5 N; D% p4 S) D6 p; Q! ~2 I
这篇博文继续分析几个小案例:
. R! R0 Y0 q& n: w$ ^% \4 q. Y, `5 o3 T5 S( W/ [
Gaussian Pulse
. T! k i# y) e( I这个案例是将高斯脉冲从时域变换到频域,高斯脉冲的信息在下面的程序中都有注释:
* ?4 Z2 A. R0 g
6 _- p. }; u) ^$ D) u6 e: q' b- 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)|')
* B' z3 s5 B' ]& K& w7 m7 \ : z- y% ]3 O2 P- z
: [/ e' W, l& f [; n
高斯脉冲在时域的图像:
- o! J s+ @2 `* S2 _" {; @, `6 T) X
- I9 s" {0 Q! m; g# p
; r1 z% E# M0 H. v5 ]( B% o% I/ B4 N- E+ e) q6 b
高斯脉冲在频域的图像:
! L& E4 R9 g. o: Z( s
$ e- ]$ \" A2 @0 O# a& L( F8 S
% N2 v1 L0 G( L- ~ v+ r: d+ P7 a" e# ]* |1 i2 k& I! k5 }. F9 t+ I
0 \/ K& j1 s. p {3 x- L! G6 _5 l. @, w
Cosine Waves
8 `8 \) L% s- c7 E, E+ U3 Z
7 \ T4 W$ O5 u+ c1 a这个例子比较简单,就是不同频率的余弦波在时域以及频域的比较:
4 E& Q( m( P( I$ ]6 \7 {" G' z' ~* G! o" a
- 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.
- % y n. J$ w* @1 ^2 `; _
- 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.
- 6 j: o0 e, S, h2 t
- 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
) w+ R: ]/ H9 F+ ?- X = [x1; x2; x3];
- % Plot the first 100 entries from each row of X in a single figure in order and compare their frequencies.
- 1 W! w/ r8 V' K% \$ p' Y6 o
- 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
4 v" Z0 }- g' V0 g9 g6 c( t& d- % 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.
! m) U5 g) `" `0 h; p, N! C$ t- n = 2^nextpow2(L);
- % Specify the dim argument to use fft along the rows of X, that is, for each signal.
- 7 p+ ^( K7 N( e( V( e! r
- dim = 2;
- % Compute the Fourier transform of the signals.
; j# ~) V: n7 \/ }- Y = fft(X,n,dim);
- % Calculate the double-sided spectrum and single-sided spectrum of each signal.
- ! b6 @/ A( }6 q3 x
- 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.
- 4 k0 e) e5 K7 t B
- 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'])
- end
+ Q/ V4 n! D. J4 x' D7 k+ G$ u ! Y0 {3 M) _+ `0 g+ ]
! Y8 b$ A( k4 k1 n: g p
下图是频率为50Hz,150Hz以及300Hz的余弦波在时域的图像:( W0 v& a! M* V) O3 n: p
& V$ B9 x3 y# [4 ?; F
# D _0 K) Y6 D( Z2 J
$ \( E# \* Q6 D; |& Z下图分别为其fft:
- Q# I/ E6 T1 w6 B/ P5 C Z3 k
# u" }; d$ A" e) A5 G
' z& v" R: Z1 s/ ]6 O. `
3 A( z8 w/ c& Q8 k6 Y* E" S从频域图中可以清晰的看到它们的频率成分位于何处。
' y" H3 P" P/ y
3 A2 B- s1 \: y& R' f; Q; H5 m7 Q9 G) \7 M- i/ l' I
|
|