10-30-2012 10:53 AM
Hello,
I am trying to implement the function specgram of matlab in Labview.
In matlab I enter a sound signal sig1 (50.000 x 2 array) with a sampling rate Fs1 = 22050.
I also define a window size of 512.
I execute b1 = specgram(sig1(:,1),windowsize,Fs1);
The result is a 2D complex double array.
Now i am trying to implement the same function in Labview.
I have already tried to use the STFT function or the FFT function in labview but I don't get close to the results I get from matlab.
Attached:
An ASCII file with the input signal: sig1.txt
Two ASCII files with the output of the matlab code:
b1real.txt --> The real part of the output
b1imag.txt --> The imaginary part of the output.
The matlab code: Matlab.txt
Thanks in advance for any kind of help
P-J
Solved! Go to Solution.
10-31-2012 04:25 AM
Hello P-J,
Are you familiar with what is happening inside of the specgram function.
On the following link you can find some more information about what this function actually does:
http://octave.sourceforge.net/signal/function/specgram.html
So replacing it by a an FFT won't directly do the trick.
Some information about this function in LabVIEW can be found over here:
- FFT: http://zone.ni.com/reference/en-XX/help/371361H-01/lvanls/fft/
As long as you're using this function, I think you should be able to get similar results.
http://zone.ni.com/reference/en-XX/help/371361H-01/lvanls/stft_spectrogram_core/
Can you show me the code you already have in LabVIEW?
This will allow us to check where it's going wrong.
Another thing that could be useful for using .m code in LabVIEW is the RT MathScript Node.
In here you can also directly use the specgram function:
http://www.ni.com/white-paper/3803/en
If you're not familiar with this, then don't hesitate to ask me for more information about it.
11-02-2012 01:39 PM
Hello,
This is the LabVIEW code I already have.
I still have a few problems with it.
The result I get from Matlab is a complex double array.
However the result in LabVIEW is a double array.
Is it possible to get a complex double array in LabVIEW?
My second problem is the array size.
In Matlab the size of the array I get is 257x194. In LabVIEW the size is 511x256.
I know the array can be transposed. But the difference between 194 and 511 is quite large.
Thanks,
P-J
11-06-2012 05:21 AM
Hello P-J,
Thanks for posting this VI.
One thing special that I saw is the following:
"specgram(sig1(:,1),windowsize,Fs1)"
It seems like you're taking the specgram of only the first column of your signal.
In the LabVIEW code you're taking an average over the three columns.
Was this your goal?
Have you already tested your code with the MathScript Node?
11-07-2012 03:11 PM
Hello,
I am indeed taking the average of the colums.
The reason is because I am working with stereo sound files. However in the STFT function, I am only allowed to insert a 1D array. So I take the average of the first 2 colums.
The result shouldn't be very different from the method used in Matlab because The two channels of the audio file are almost the same.
But my biggest concern is to get a similar result of my Matlab code in LabVIEW.
Mathscript is no option for me because I am a University student working on a speach recognition project.
I am not allowed to install/use Mathscript.
Thanks,
P-J
11-11-2012 07:10 PM - edited 11-11-2012 07:11 PM
Hello P-J,
I don't completely get your logic.
It seems that I'm missing something in your explanation.
In your original matlab-code you're performing the specgram function on the 1D array sig(:,1).
Therefor in LabVIEW it would be logic that you want to perform a similar function on the same 1D array.
Otherwise any comparison results will not make alot of sense.
Please also note that it's very dangerous to say that audio signals are the same on the left and the right side of a stereo-signal.
This often is not the case and could lead to incorrect interpretations of correct measurements.
Are you using the specgram function from the Signal Processing Toolbox?
http://www.engmath.dal.ca/courses/engm6610/notes/specgram.html
Or are you using another version of the specgram function?
If you're using this version of the specgram function, then the special length of the matlab-function can be explained by this line on the website above:
If a is real, specgram computes the discrete-time Fourier transform at positive frequencies only. If n is even, specgram returns nfft/2+1 rows (including the zero and Nyquist frequency terms). If n is odd, specgram returns nfft/2 rows. The number of columns in B is
k = fix((n-numoverlap)/(length(window)-numoverlap))
This completely explains the number of rows you get over here.
On the other side the STFT function will calculate a short time Fourrier transform (http://en.wikipedia.org/wiki/Short-time_Fourier_transform)
This is different from the DTFT (http://en.wikipedia.org/wiki/Discrete-time_Fourier_transform) used in your original specgram function.
I think you might have overlooked these differences.
A short summary of different kinds of FT's can be found over here:
http://narnia.cs.ttu.edu/drupal/node/46
Is there a specific reason why you need to have exactly the same calculation?
Coming back to what is going wrong in your VI I can explain this quite easily.
First take a look at this page:
http://zone.ni.com/reference/en-XX/help/371361H-01/lvanls/stft_spectrogram_core/
Over here you see that they give the following definition:
time steps specifies the number of samples to shift the sliding window.
This should not be -1 if you want to have a similar calculation. It should be equal to the numoverlap parameter used in the specgram function.
After a short calculation you can find that in your original specgram calculation this was 256.
Please also redo this calculation at your side and check if you get a similar result.
One last word about the comparison is the following:
- In LabVIEW the STFT function returns the actual magnitude of the spectrogram.
- The specgram function returns the windowed discrete-time Fourier transform of a signal using a sliding window. The actual spectrogram is the magnitude of this function.
In your last e-mail you note that you are not allowed to install/use MathScript.
Are you sure you’re not confusing Matlab (non-NI software) with Mathscript (NI software: http://sine.ni.com/nips/cds/view/p/lang/nl/nid/207300) ?
Most likely the MathScript RT Module is already installed with your LabVIEW installation.
If it’s not, then most likely you should be allowed to use it with your license.
Can you share your serial number privately with me?
That way I can check if you should be allowed to use the MathScript RT Module.