# How can I plot a spectrogram plotting time vs instantaneous frequency vs amplitude from an exported output signal?

46 views (last 30 days)
Syed Abid Sahdman on 21 Jul 2024 at 0:41
Edited: Umar on 22 Jul 2024 at 18:52
I have a text file with time(1st column) and voltage amplitude (2nd column). How do I plot a spectrogram plotting time vs instantaneous frequency vs amplitude? Could you please help me plot it? I will really appreciate it.
I have attached the text file and a picture of the time domain representation of the signal.
Syed Abid Sahdman on 22 Jul 2024 at 17:03
Edited: Syed Abid Sahdman on 22 Jul 2024 at 17:14
Hello Umar,
Thank you so much for your help!
Could you please tell me how to determine the value of the parameters of spectrogram function? Also, does the spectrogram function transform the amplitude to power/frequency? Is there a way to plot just the amplitude?
Also, could you please tell me why we are getting negative instantaneous frequency and how to fix that? Thanks.
Umar on 22 Jul 2024 at 18:49
Edited: Umar on 22 Jul 2024 at 18:52
Hi Syed,
Question: Could you please tell me how to determine the value of the parameters of spectrogram function? Also, does the spectrogram function transform the amplitude to power/frequency?
To answer this question, you have to understand how this function works, please refer to
https://www.mathworks.com/help/signal/ref/spectrogram.html
Please bear in mind that this function computes the power spectral density (PSD) of the signal. This means that the amplitude values are transformed into power values, representing the distribution of power across different frequencies over time.
Question: Is there a way to plot just the amplitude?
If you wish to plot just the amplitude values without converting them to power, you can modify the spectrogram calculation slightly. Instead of using the spectrogram function directly, you can compute the Short-Time Fourier Transform (STFT) manually and plot the magnitude of the resulting spectrogram. For more information on STFT, please refer to
https://www.mathworks.com/help/signal/ref/spectrogram.html#mw_c6dfa97d-9a98-4238-a6bd-d58825fb47e0
Question: could you please tell me why we are getting negative instantaneous frequency and how to fix that?
So, I did some experimentation with code to find solution by using unwrapping of the phase and then realized how to overcome this issue. I had to adjust the calculation of instantaneous frequency to ensure that only positive values are obtained. The negative values in the plot were likely due to the unwrapping of the phase and subsequent calculation of the instantaneous frequency. Here is the snippet code to help this issue.
% Calculate instantaneous phase
instantaneous_phase = unwrap(angle(hilbert(amplitude)));
% Adjust phase to prevent negative frequencies
positive_phase = mod(instantaneous_phase, 2*pi);
% Calculate corrected instantaneous frequency
instantaneous_frequency = diff(positive_phase) / (2 * pi) / (time(2) - time(1));
% Ensure positive values for instantaneous frequency
instantaneous_frequency(instantaneous_frequency < 0) = 0;