我有一个信号向量 x(t) 及其时间向量。我想获得信号的频率表示,特别是 x(t) 的能谱。
有人可以展示一些光来获得能谱吗?能谱和能谱密度,ESD是否产生相同的结果?
任何帮助深表感谢 !
t = 0:.001:.25;
x = 5*sin(2*pi*50*t) + 1.2*sin(2*pi*120*t);
y = x + 2*randn(size(t));
y=y';
Fs = 1000; % Sampling frequency
% Fast Fourier Transform
L = length (y); % Window Length of FFT
nfft = 2^nextpow2(L); % Transform length , Next power of 2 from length of signal
% Exactly same as nfft = pow2(nextpow2(L))
% Take fft, padding with zeros so that length(input) is equal to nfft
Ydft = fft(y,nfft)/L;
y_HamWnd = y.*hamming(L); %hamming window fft
Ydft_HamWnd = fft(y_HamWnd,nfft)/L;
%FFT is symmetric, throw away second half
% Take the magnitude of fft of x
%Amplitude Spectrum 01 (Single Sided)
mY = 2*abs(Ydft(1:nfft/2+1)) ;
%Amplitude Spectrum 02 (Single Sided HamWmd)
mY_HamWnd = 2*abs(Ydft_HamWnd(1:nfft/2+1));
%Amplitude Spectrum 03 (Single Sided; Exclude DC-- FoldingFreq)
mY2 = abs(Ydft);
mY2= mY2(1:nfft/2+1);
mY2 (2:end-1) = 2* mY2 (2:end-1);
%To get the Power
% P ; single sided; then coefficient square**********Multiply by 2 ?
YdftCoeffi_Sqr = Ydft(1:nfft/2+1).^2;
% P = y*(conj y) == y^2 ; %********DO WE NEED TO MUL 2 ?
Pyy_NormNfft = Ydft.*conj(Ydft);
% P ; single sided; First square, then Multiply by 2
m1YL = abs(Ydft(1:nfft/2+1));
mSqrYL = m1YL.^2;
m2YL = mSqrYL*2;
% Frequency vector
f1 = Fs /2 * linspace(0,1, nfft/2 + 1);
f2 = (0:nfft-1)*(Fs/nfft);
% Generate the plot, title and labels.
figure;
plot(t,y); title('Time Domain'); xlabel('Time(s)');
figure;
subplot(3,1,1)
plot(f1,mY)
title('Amp Spec 01 --mY = 2*abs(Ydft(1:nfft/2+1)) /L; '); xlabel('Frequency (Hz)'); ylabel('|mY|')
%axis ([0 500 0 5]); %Zoom in
subplot(3,1,2)
plot(f1,mY_HamWnd)
title('Amp Spec 02 -- mYHamWnd =2*abs(YdftHamWnd(1:nfft/2+1))/L;'); xlabel('Frequency (Hz)'); ylabel('|mY_HamWnd|')
%axis ([0 500 0 5]); %Zoom in
subplot(3,1,3)
plot(f1,mY2)
title('Amp Spec 03 DC-- FoldingFreq -- mY2 ;First, single sided; then square, lastly Multiply by 2'); xlabel('Frequency (Hz)'); ylabel('|mY2|')
%axis ([0 500 0 5]); %Zoom in
figure;
subplot(3,1,1)
plot(f1,YdftCoeffi_Sqr);
title('Ydft Complex Coefficient Square; Single Sided-- YdftCoeffi_Sqr = Ydft(1:nfft/2+1).^2'); xlabel('Frequency (Hz)'); ylabel('YdftCoeffi_Sqr');
subplot(3,1,2)
plot(f1,m2YL);
title('Ydft Complex Coefficient Magnitude Square; Single sided; then Multiply by 2');
xlabel('Frequency (Hz)');
ylabel('Power m2YL');
%axis ([0 500 0 5]); %Zoom in
