I am trying to make a spectrogram viewer without using the spectrogram command. For this purpose, I used a sin function. I broke up the input signal into 256 segments, multiplied each segment with the Hamming window, took the FFT of each slice and then stored them to obtain spectra of the signal.
My problem is that I couldn't get the exact spectrum. It is probably due to the fact that I could not divide input signal into overlapping segments?
This is my code: Can someone help me where I am doing it wrong?
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 10240; % Length of signal
NFFT=10240; % number of fft points
t = (0:L-1)*T; % Time vector
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
X = hann(L).*x';
figure
plot(X)
k=256;
for idx = 1:length(x)/k
%take a slice from the input vector
slice = x(1+(idx-1)*k:idx*k);
%multiply it with the window and transform it into frequency domain
spectrum = fft(slice'.*hamming(k),k);
%get the spectrum magnitude at each of the 256 frequency points and store it
mag_spectrum(:,idx*1024) = abs(spectrum).^2;
end
figure
w=hamming(k);
plot(w)
figure
plot(t,X,'k');
xlabel('Time (sec)','FontSize',14);
ylabel('\it{x(t)}','FontSize',14);
% spectrogram(x,128);
% SET PARAMETERS
figure
plot(slice); % To look for it is windowed or not which is not in my case?
%figure
%plot(mag_spectrum);
figure
spectrogram(X,256); % Real spectogram
No comments:
Post a Comment