How to change time to amplitude in a spectrogram?

6 views (last 30 days)
Hi experts,
I have created an 11k wav file and am reading it into MATLAB. From here I am using the data given from the wav file and using a spectrogram to view it.
When I run the code, the spectrogram returned is given in time and frequency. The frequency returned is correct. Is it possible to change the time to amplitude? this would be very useful for my results.
Here is my code :
clear; [y, fs] = audioread('11k_wav.wav'); figure(1); spectrogram(y,256,224,512,fs,'xaxis');
Thank you in advance.

Accepted Answer

Star Strider
Star Strider on 10 Mar 2016
Amplitude in a spectrogram is the z-axis value. The pectrogram is actually a surf plot, so you can use the interactive image rotation in the plot GUI or the view function to make the amplitude correspond to the y-axis.
  23 Comments
Noel Sharpe
Noel Sharpe on 22 Apr 2016
so due to research and trials and error I have gotten the plot of the phase to certain point. This point is having a spike at the correct frequency. The phase is still incorrect. Can you see where my error is and a suggestion on how to fix this error please?
here is my code and resulting plot:
fs = 44100; %setting the sampling frequency of music
t= 0:1/fs:0.01; % setting the step size with the end lenght = 10ms (1 chunk)
n = 441; %no of samples for fat under recommendation
g = sin(2*pi*17800*t+pi); %+ 0.5*sin(2*pi*18000*t+pi/2) + 2*sin(2*pi*19000*t+3*pi/2);% creating the sine wave with phase offsets
fftres = fft(g,n); %using the fft on the sine wave with n number of samples
% figure(3);
plot(real(fftres)) % plot the real part of the vector
grid on % turn on the grid for data points and clarity
ylabel('Real') % set label for y axis
title('Raw FFT spectrum') % set label for title
figure(6); %Calculate the phase versus frequency
phase = angle(fftres);
imag = imag(fftres)*180/pi;
plot(frequencyAxis,imag);
xlabel ('Hz');
ylabel ('Phase');
grid on;
Thank you.
Star Strider
Star Strider on 22 Apr 2016
I needed to make a few changes:
fs = 44100; %setting the sampling frequency of music
t= 0:1/fs:0.01; % setting the step size with the end lenght = 10ms (1 chunk)
n = 441; %no of samples for fat under recommendation
g = sin(2*pi*17800*t+pi); %+ 0.5*sin(2*pi*18000*t+pi/2) + 2*sin(2*pi*19000*t+3*pi/2);% creating the sine wave with phase offsets
fftres = fft(g,n); %using the fft on the sine wave with n number of samples
frequencyAxis = linspace(0, 1, fix(length(fftres)/2)+1)*fs/2; % <— Created My Own Version
Iv = 1:length(frequencyAxis);
figure(3);
plot(frequencyAxis,abs(fftres(Iv))) % plot the real part of the vector
grid on % turn on the grid for data points and clarity
ylabel('Real') % set label for y axis
title('Raw FFT spectrum') % set label for title
figure(6); %Calculate the phase versus frequency
phase = angle(fftres);
imag = imag(fftres)*180/pi;
plot(frequencyAxis,phase(Iv));
xlabel ('Hz');
ylabel ('Phase');
grid on;
First, you need to plot the absolute value to get the magnitude.
Second, the phase should reverse at the resonant frequency, and it does. (In a tuned circuit, the impedance goes from leading, or capacitive, to lagging, or inductive, at resonance, and becomes purely resistive at resonance, so the phase is zero there.) You can prove that easily enough analytically (much easier if you have the Symbolic Math Toolbox).
The phase looks correct to me, and goes to zero at the same frequency as the peak magnitude of the absolute value of the Fourier transform.
Third, the imaginary part of the fft is simply an amplitude, not an angle, so multiplying it by 180/pi simply scales it.
Fourth, please do not use ‘imag’ as a variable name. That is the name of the function (the same function you used to create it), so MATLAB will refer to the variable, not the function, if you call it later in your code. This is called ‘overshadowing’. Avoid it.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!