|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
, f0 E ^: `3 m, J& o
上篇:MATLAB —— 信号处理工具箱之fft的介绍和相关案例分析介绍了MATLAB信号处理工具箱中的信号变换 fft 并分析了一个案例,就是被噪声污染了的信号的频谱分析。, A2 w8 ?# m5 h. ?" j' y
2 J& s& f. h, e这篇博文继续分析几个小案例:
2 z2 }- w: t; T t0 z& J3 x! ~3 e* r6 f7 H& Q" F7 V
Gaussian Pulse+ Z6 ?+ c M' u4 p
这个案例是将高斯脉冲从时域变换到频域,高斯脉冲的信息在下面的程序中都有注释:
( e/ t: ~7 F2 x3 O2 t J3 X- ]3 L' r( 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)|')
8 K' ], Y) {7 X, c% X
/ h( E# a8 h2 J4 `! _
7 F. ] G$ g1 a. S: t8 x高斯脉冲在时域的图像:
; A2 V+ `! C9 K9 T8 Q( X) d7 ]+ L: L0 q* T! E; `
, T7 o$ }, Y& z3 u6 h& q, m$ R9 _/ Y2 }) s* {% x4 B
高斯脉冲在频域的图像:+ N4 b* l( D0 t) U5 K3 R8 v
& ~4 k/ `" @6 F8 A/ r
6 g% x( g5 {3 b) V. _6 E7 C: v3 h L
8 ~. X$ R- d& u8 v6 U! V9 A2 |, b
) J( v/ U6 V9 X+ c! m. L( C& a& r5 m8 H
Cosine Waves
1 G9 w& K1 A: ^9 P v
P1 O, H( j6 I. W$ N$ v这个例子比较简单,就是不同频率的余弦波在时域以及频域的比较:0 v) `- Y% T) y" n
9 U9 @+ A* R, j# H0 R* 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.
- : N5 |2 W4 v' x
- 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.
/ x5 h6 B' m& l* T: t) N' ^5 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
- v( E1 J& j" H0 r8 Y; X" s8 R! t' b
- X = [x1; x2; x3];
- % Plot the first 100 entries from each row of X in a single figure in order and compare their frequencies.
- $ T" X0 |) ?' P2 f# r0 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
6 a; J2 N% G8 @5 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.
- , k9 o6 r! e5 Q
- n = 2^nextpow2(L);
- % Specify the dim argument to use fft along the rows of X, that is, for each signal.
- # d7 _/ {2 `8 B
- dim = 2;
- % Compute the Fourier transform of the signals.
- 9 V# Q( s. X/ |- x* k% {5 G, W6 T
- Y = fft(X,n,dim);
- % Calculate the double-sided spectrum and single-sided spectrum of each signal.
, a( I1 Y$ q7 a, m6 ~ \- 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.
- , F" n% O- z6 w5 d2 E( [
- 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$ P; z, d( X( s _ j9 w
5 j% h( P1 E. S( w: H7 ^6 e0 S: F
9 n! }8 l3 [" j% L# z6 T- G' Q) W
下图是频率为50Hz,150Hz以及300Hz的余弦波在时域的图像:
n1 ~7 l9 g! ~2 r# d) w4 k+ m! H, u$ }* g6 e6 b
5 A! r0 E' S! R0 G6 R7 f8 ?; Y7 P% @1 a0 n
下图分别为其fft:) K8 h: v- X0 t4 w0 Y
8 z8 j/ S& F! _" S6 L
- a3 E: S: y" J% Z7 N: `# I k; k) L
从频域图中可以清晰的看到它们的频率成分位于何处。
2 F3 v- f* T; c
7 }( W K( ? [
& U) P! {: Z! _" z" }1 U9 i |
|