|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
# i. O% v7 v' g& [$ I. `
上篇:MATLAB —— 信号处理工具箱之fft的介绍和相关案例分析介绍了MATLAB信号处理工具箱中的信号变换 fft 并分析了一个案例,就是被噪声污染了的信号的频谱分析。
& M% n; Y# e( z; w: E
" b1 o2 ~- U1 @% f这篇博文继续分析几个小案例:3 j0 E& x: H8 C: @ j2 C$ n7 x6 I
+ ^- x* }) U% s* x* w9 V. ~Gaussian Pulse- R0 i) c. v: X9 j# d
这个案例是将高斯脉冲从时域变换到频域,高斯脉冲的信息在下面的程序中都有注释:
6 z6 H( i1 j5 z6 j1 D8 F
( t8 S, N3 ^5 l9 E- 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)|')
- 9 J5 \6 W4 m# X% F9 Y2 o
% R1 N& g% L2 H0 \
' O \; T/ G/ C高斯脉冲在时域的图像:$ B+ `/ R$ h ?) `" J
2 r8 [3 s6 G+ j* V2 ?; D, S
. L5 n. ^' D% L) l" e( q* E" q3 c
高斯脉冲在频域的图像:/ L- S0 Q7 n' }: m3 b
3 Z, e9 Y& l7 Q* p' Z
0 Q+ W9 Z! T f5 }5 s9 W) F% k5 a: V x
4 J) c- ~; }) f( Z' p/ i
5 ^% g' w5 f6 y. \& x0 `/ ECosine Waves
; n) v0 c" Z; k0 w* Z* @' l+ |+ l2 ?: x; F) N0 E$ t9 l8 q
这个例子比较简单,就是不同频率的余弦波在时域以及频域的比较:
. d. `$ j6 G. U8 V0 q
. g) A/ s6 }0 E2 u$ q; ~- 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.
8 s1 r9 L: e3 @. W6 z( m) q& p& w- 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.
' ^1 R+ {7 O$ n/ t/ s* S6 W- U- s- 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
Q/ ]" P& r$ E2 h0 N, y- X = [x1; x2; x3];
- % Plot the first 100 entries from each row of X in a single figure in order and compare their frequencies.
5 ~+ ?' u* q4 ]1 {- 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
. \: i: [' u1 `' n2 s# A: H- % 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.
8 Z5 L' A+ e& ^7 Z& F3 K8 E- n = 2^nextpow2(L);
- % Specify the dim argument to use fft along the rows of X, that is, for each signal.
2 t; p! k- k4 R7 c, v* u/ W- dim = 2;
- % Compute the Fourier transform of the signals.
- " n( o+ H4 C/ I
- Y = fft(X,n,dim);
- % Calculate the double-sided spectrum and single-sided spectrum of each signal.
# @1 {7 ?- R4 v0 @8 O- 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.
- * q5 F; h- T% p4 p# y2 a! w& u5 F
- 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
" j3 s4 |' l2 q6 l3 ?) @
# Y5 i/ r4 W- T, r! M 9 f# |# I) m# [/ S2 b9 i
下图是频率为50Hz,150Hz以及300Hz的余弦波在时域的图像:
- O7 {/ i9 C E8 w6 P6 a* B) {9 d4 F/ c
5 t( ^7 ~: }5 H) j2 B
( z- y3 w" g5 |+ o6 B- b下图分别为其fft:
; t9 c4 A S% y( P
) u% p4 U8 M& j4 X* y+ Y
3 C9 U( b7 C. f8 [4 D, B% Q# F2 P! ~
从频域图中可以清晰的看到它们的频率成分位于何处。2 g& X2 R0 ^5 O# y4 k( i
1 X- S" p" d+ p2 V$ K7 y# d, T Q6 i+ R/ h8 f
|
|