|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
1 M% \7 m) R" V% Q9 ?0 r" H
上篇:MATLAB —— 信号处理工具箱之fft的介绍和相关案例分析介绍了MATLAB信号处理工具箱中的信号变换 fft 并分析了一个案例,就是被噪声污染了的信号的频谱分析。" s6 H F4 _3 ~# n4 D/ x9 a
/ O7 f9 G; O, l' H5 x
这篇博文继续分析几个小案例:
% `0 X7 `: Z- s5 v- \9 u
: U) j& S9 }% h2 B7 B1 sGaussian Pulse
5 Z/ ] } m5 q& C6 X. B' B3 Z这个案例是将高斯脉冲从时域变换到频域,高斯脉冲的信息在下面的程序中都有注释:- x+ w; v$ I; e) |" w7 D8 D/ [
6 a" K2 w0 Y. Y0 ]1 b3 ^$ V( d
- 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)|')
- $ f$ ^2 A2 J+ C+ j
- r2 R! {, P5 o- h, P, c {
4 C" o3 J! b( G1 i0 Z7 Z1 ~7 Q高斯脉冲在时域的图像:7 r) h8 X/ x4 O/ Z' e
8 l- m& L/ s3 `' X, k
0 e" X8 T2 c d) E$ s; l! W( P( [
, b4 q5 b! H# q# [; @
高斯脉冲在频域的图像:, A0 a, D9 N M7 Y+ |
9 d, u' {% ^ d0 C( u
9 ?* F1 ]2 Y: `) b0 c3 G; ^
% y- J# T! w) y0 l& i
2 C2 c0 ^- E1 `- }! e/ q; F
( A1 b: Y& b! |* F' MCosine Waves
2 g1 O- C' b3 T# E! B/ A) _3 b% w9 A7 [0 ~! {" L6 }0 H
这个例子比较简单,就是不同频率的余弦波在时域以及频域的比较:4 Z) g9 W# A) o7 O/ p
! }! {1 h) [) a. A; k6 M- 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.
- & f) T: Z4 G9 I3 q* v2 Q
- 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.
- 8 X4 H5 d# U. o/ w# y
- 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
- % v' O V9 E2 F6 ^- {
- X = [x1; x2; x3];
- % Plot the first 100 entries from each row of X in a single figure in order and compare their frequencies.
! g; i$ ?( T( z, f0 [/ |9 [2 E- 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
+ W' w+ i$ n1 _6 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.
- # W+ G) z" a$ y0 t+ H8 y
- n = 2^nextpow2(L);
- % Specify the dim argument to use fft along the rows of X, that is, for each signal.
$ \8 _+ Z/ @4 K% t9 ^, d- dim = 2;
- % Compute the Fourier transform of the signals.
7 a) i% F, s! ?2 `0 N1 w- e- Y = fft(X,n,dim);
- % Calculate the double-sided spectrum and single-sided spectrum of each signal.
- ) a/ _1 P! H T$ h
- 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.
$ m- ?+ ^! o" r, S6 n& a- 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'])
- end7 x; O5 L$ x, o& Q" a2 T
: f% F# _( U1 ] Y0 y! Y0 ^2 M
u+ ?9 L1 }( _) B2 C# W下图是频率为50Hz,150Hz以及300Hz的余弦波在时域的图像:
/ D, K8 W$ O" H: q1 R" {1 C+ U4 b: [6 s
! Z. L7 q, K( X+ _/ t5 Y p' V% d; q
! w W4 }7 h: Y1 P5 g
下图分别为其fft:* `1 t) d! e* x
2 }2 u" g2 G2 v5 R6 A$ e
# O' F% w6 e1 n) E8 p
: B% \ f1 f6 M7 F @. l: I从频域图中可以清晰的看到它们的频率成分位于何处。8 z( H. H( S! u
' l( ~4 i, Y; f2 u" ^' _& H/ R0 ]9 G' ^% }0 q# x6 ^
|
|