2012-03-12 37 views
0

一個數字,這是我的matlab源代碼錯誤密謀在MATLAB程序

% let 'y' is a workspace file containg sampled data from DSO. 
fs=2500; %sampling freq 
T=1/fs;  % sample time 
L=2500; % length of the signal 
t=(0:L-1)*T; %time vector 
NFFT=2^nextpow2(L); %next power of 2 from length of y. 
         %p = nextpow2(y) returns the smallest power of two that is greater than or equal to the absolute value of L. 
         %(That is, p that satisfies 2^p >= abs(L)). 
         %This function is useful for optimizing FFT operations, which are most efficient when sequence length is an exact power of two. 
         %If A is non-scalar, nextpow2 returns the smallest power of two greater than or equal to length(L). 
Y=fft(y,NFFT)/L; 
f=fs/2*linspace(0,1,NFFT/2); 
% to plot signal-sided amplitude spectrum. 
plot(f,2*abs(Y(1:NFFT/2))) 
title('Single-sided amplitude spectrum of y(t)'); 
xlabel('Frequency (Hz'); 
ylabel('|Y(f)'); 
grid; 
bar(f,2*abs(Y(1:NFFT/2)),'BarWidth',1) 

我有從數字示波器&我想FFT以條形圖形式的採樣數據。

繪製圖後不同標籤&標題&網格也沒有出現。

回答

2

問題是你正在繪製你的初始情節。

% to plot signal-sided amplitude spectrum. 
plot(f,2*abs(Y(1:NFFT/2))) % create a plot 
title('Single-sided amplitude spectrum of y(t)'); % annotate it nicely 
xlabel('Frequency (Hz'); 
ylabel('|Y(f)'); 
grid; 
% throw away all that nice data and plot and plot over it with this: 
bar(f,2*abs(Y(1:NFFT/2)),'BarWidth',1) 

試試這個:

figure; 
% to plot signal-sided amplitude spectrum. 
plot(f,2*abs(Y(1:NFFT/2))) 
title('Single-sided amplitude spectrum of y(t)'); 
xlabel('Frequency (Hz'); 
ylabel('|Y(f)'); 
grid; 
figure; 
bar(f,2*abs(Y(1:NFFT/2)),'BarWidth',1) 

此外,位,所以建議。你會注意到Oli編輯了這個。他可能會給你答案,但決定不會因爲0%接受率。請回到一些其他問題並關閉它們。如果這個答案有幫助,請接受它。 ;-) 謝謝!

+0

謝謝..我得到了正確的plot.now我想從圖中顯示「f」和「(Y(1:NFFT/2)」的值。 – user991852 2012-03-12 18:52:20