ftt data for waterfall plot - matlab

I used periodogram in Matlab to perform vibration analysis. I wanted to see how the frequencies changed with time during vibration, and I believed I should use waterfall plot or other 3D plot. My question is how I can save the amplitude data from ftt into a matrix (m,n). m is number of the data point from each calculation, n is the number of time interval for each calculation with periodogram.
I learned how to plot waterfall plot or other 3D plot in Matlab, but do not know how to save the amplitude data into a matrix. Did some search, but with no luck.

Related

3-D Plot in MATLAB Containing: Time, Frequency and Power Spectral Density

I am currently working on a project for my Speech Processing course and have just finished making a time waveform plot as well as both wide/narrow band spectrograms for a spoken word in Spanish (aire).
The next part of the project is as follows:
Make a 3-D plot of each word signal, as a function of time, frequency and power spectral density. The analysis time step should be 20ms, and power density should be computed using a 75%-overlapped Hamming window and the FFT. Choose a viewing angle that best highlights the signal features as they change in time and frequency.
I was hoping that someone can offer me some guidance as to how to begin doing this part. I have started by looking here under the Spectrogram and Instantaneous Frequency heading but was unsure of how to add PSD to the script.
Thanks
I am going to give you an example.
I am going to generate a linear chirp signal.
Fs = 1000;
t = 0:1/Fs:2;
y = chirp(t,100,2,300,'linear');
And then, I am going to define number of fft and hamming window.
nfft=128;
win=hamming(nfft);
And then I am going to define length of overlap, 75% of nfft.
nOvl=nfft*0.75;
And then, I am performing STFT by using spectrogram function.
[s,f,t,pxx] = spectrogram(y,win,nOvl,nfft,Fs,'psd');
'y' is time signal, 'win' is defined hamming window, 'nOvl' is number of overlap, 'nfft' is number of fft, 'Fs' is sampling frequency, and 'psd' makes the result,pxx, as power spectral density.
Finally, I am going to plot the 'pxx' by using waterfall graph.
waterfall(f,t,pxx')
xlabel('frequency(Hz)')
ylabel('time(sec)')
zlabel('PSD')
The length of FFT, corresponding to 20ms, depends on sampling frequency of your signal.
EDIT : In plotting waterfall graph, I transposed pxx to change t and f axis.

Frequency from fft data set matlab [duplicate]

This question already has answers here:
How do I obtain the frequencies of each value in an FFT?
(5 answers)
Closed 6 years ago.
I have a data set in a matrix in matlab. It contains 25,000 values taken every 0.5 ns; so the total time of the dataset is 1.25E-5 seconds.
The data set contains very high frequency noise that I am not interested in so I create another matrix is every 50th data point from the first matrix So the size of the matrix is 1000*.
I plot the absolute values from matlab's fft this matrix (I also normalise the amplitude and only plot the first half) and get the attached (two plots, second is a close up of the low frequencies I am interested in). How do I convert the x-axis to frequency?
Another point, if I take every data point (so I create an fft of the entire 25,000 points) then the x-axis is exactly the same; in other words, the size of my matrix seems to have no bearing on the x-axis returned by matlab. I've attached two links to the frequency spectrum, one of which is a close-up of the low frequencies I am interested in. It's axis goes from 0-50, so it is these values I need to convert to Hz.
Thankyou in advance!
Close up of frequency spectrum
frequency spectrum
From what I read on http://www.mathworks.com/help/matlab/math/fast-fourier-transform-fft.html#bresqop-1, it appears that the units on the x-axis of the plotted FFT are Hz if the first vector, f, you put into the plot(f,power), is defined as a sequence of n elements (n being the number of data points put into the FFT) increasing from zero to the sample frequency.
Thus, for the first plot, which used every 50th of points that were taken at a frequency of 2 GHz, the sample frequency would be 40 MHz. Thus, f = (0:n-1)*4*10^7/(25000/50)
It goes on to show how to use the fftshift function to put the center of the output of the fft function at 0, but it's clear you already did that and chopped off the negative part.
So, once you have the right separation of fs/n, sampling frequency divided by number of data points used, in the vector that supplies the x-axis to the plot function, then the units of the x-axis will be Hz.
(I hope you still have the numbers to graph again? If not, this question might help: Confusion in figuring out the relation between actual frequency values and FFT plot indexes in MATLAB)

Confusion in figuring out the relation between actual frequency values and FFT plot indexes in MATLAB

I know that there are a lot of similar questions to this, I am still unable to figure out the answer.
Let's say we have time signal in MATLAB:
t=0:1/44100:1
and a cosine signal with frequency 500Hz:
x=cos(2*pi*500*t);
Now, I am trying to plot the magnitude spectrum obtained using the fft command on signal x
FFT=abs(fft(x))
plot(FFT)
According to the theory, we should get two peaks in the plot, one at -500 Hz and the other at 500Hz.
What I don't understand is that I do get two peaks but I can't figure out at what frequencies these peaks are. I know there is a way to figure out the frequency using the FFT index, length of the input signal and the sampling frequency but I still can't calculate the frequency.
I know that there are methods to align the FFT plots so that the peaks lie at the index number of the frequency they represent by using the fftshift function, but what I want is to figure out the frequency using the the plot resulting from simply calling this function:
FFT=fft(x)
In this case, I already know that signal contains a cosine of 500Hz, but what if the signal that we want to get the FFT of is not known before time. How can we get the frequency values of the peaks in that sample using the output from the fft function?
You need to generate the frequency array yourself and plot your FFT result against it.
Like this:
function [Ycomp, fHz] = getFFT(data, Fs)
len = length(data);
NFFT = 2^nextpow2(len);
Ydouble = fft(data, NFFT)/len; % Double-sided FFT
Ycomp = Ydouble(1:NFFT/2+1); % Single-sided FFT, complex
fHz = Fs/2*linspace(0,1,NFFT/2+1); % Frequency array in Hertz.
semilogx(fHz, abs(Ycomp))
end
You will see peaks at 500 Hz and Fs - 500 Hz (i.e. 44100 - 500 = 43600 Hz in your particular case).
This is because the real-to-complex FFT output is complex conjugate symmetric - the top half of the spectrum is a "mirror image" of the bottom half when you are just looking at the magnitude and is therefore redundant.
Note that of plotting power spectra you can usually save yourself a lot of work by using MATLAB's periodogram function rather than dealing directly with all the details of FFT, window functions, plotting, etc.

Plotting time vs frequency in matlab

I am trying to find a way to create a graph that shows an audio wave and its time-frequency data (time on x-axis, wave energy and frequency data on y-axis). I have this code that does it on two separate plots:
[audio, fs] = wavread('audio.wav');
subplot(211)
spectrogram(audio,256,200,256,fs,'yaxis')
subplot(212)
wavEnergy=audio(:,1);
time=(1/fs)*length(wavEnergy);
t=linspace(0,time,length(wavEnergy));
plot(t,wavEnergy);
and now I need help with 2 things.
First, how do I get the spectrogram time to be in terms of seconds? Right now it graphs with an x-range of 0-340 (labeled 'time') and I know the clip is about 40s long (the other plot properly displays this).
Second, how do I plot them together? I know I can get a matrix from spectrogram but which array do I get from that matrix and how do I convert its timeframe to seconds?
EDIT:
First problem solved, but the graphs are still doing something strange - they both output about 40s of data, but the ranges of the graphs and the offsets of the data are different. The spectrogram goes from 0s-40s but the first .5s shows no data, and the wave plot goes from 0s-45s and the last 5s shows no data. How can I get the ranges and offsets to be the same?
EDIT 2:
I just needed to use axis tight; on both subplots
Aligning these two plots on the same time-base relies on determining the sampling frequency of your data. Based on the parameter you are passing to spectrogram, the sampling frequency is 1000 Hz. Based on your definition of time = (1/8000)*length(wavEnergy), the sampling frequency is 8000 Hz. These are not consistent. To get the audio sampling frequency from your wav file you can use [audio, fs] = wavread('audio.wav').

Plotting Average Spectra Plot using MATLAB

I have four 1xN sound signals and I want to view the average spectra plot like the one given in the link below:
http://i1233.photobucket.com/albums/ff396/sakurayen/Plot/AMaximumLikelihoodApproachtoSinglechannelSourceseparationpdf-AdobeReader.jpg
I've tried to use the MATLAB function , PSD, to plot the spectral but I am getting a different plot instead. Note that the data used for both the plots are the same.
plot obtained using PSD function in MATLAB:
http://i1233.photobucket.com/albums/ff396/sakurayen/Plot/PowerSpectralDensityofRJMF.png
Thanks!