How can I plot a spectrogram plotting time vs instantaneous frequency vs amplitude from an exported output signal?
5 Comments
Hi Syed,
You asked, “ I am getting negative frequuency in the instantaneous frequency plot. Why is that and how to fix that? Also, I am not gettiing an accurate spectrogram and I am not sure what the color is representing, supposed to be amplitude. Could you please help me fix it?”
I made modifications to your code, please see updated code along with your questions answered.
% Load data from the text file using textscan
fileID = fopen('Oscillatory.txt', 'r');
data = textscan(fileID, '%f %f', 'HeaderLines', 1); % Assuming the first line is a header
fclose(fileID);
% Access the loaded data
time = data{1};
amplitude = data{2};
% Calculate instantaneous frequency
instantaneous_frequency = diff(unwrap(angle(hilbert(amplitude))) / (2 * pi)) / (time(2) - time(1));
% Plot amplitude vs time
subplot(3, 1, 1);
plot(time, amplitude);
xlabel('Time (ns)');
ylabel('Amplitude');
title('Amplitude vs Time');
% Plot instantaneous frequency vs time
subplot(3, 1, 2);
plot(time(2:end), instantaneous_frequency);
xlabel('Time (ns)');
ylabel('Instantaneous Frequency');
title('Instantaneous Frequency vs Time');
% Plot spectrogram
subplot(3, 1, 3);
spectrogram(amplitude, 128, 120, 128, 1/(time(2)-time(1)), 'yaxis');
title('Spectrogram: Time vs Instantaneous Frequency vs Amplitude');
% Adjust subplot spacing
sgtitle('Plots of Amplitude vs Time, Instantaneous Frequency vs Time, and Spectrogram');
Please see attached snippet code along with plot.


To begin with, the above code snippet loads data from your attached text file named 'data.txt' into two arrays: time and amplitude. The time array stores the time values, while the amplitude array stores the corresponding amplitude values.Next, the code calculates the instantaneous frequency using the Hilbert transform. The hilbert function computes the analytic signal of the amplitude array, and unwrap and angle functions extract the phase information. By taking the difference of the unwrapped phase divided by 2π and the time difference, the instantaneous frequency is obtained.After the calculations, the code proceeds to plot the data in three subplots. The first subplot displays the amplitude vs. time, the second subplot shows the instantaneous frequency vs. time, and the third subplot visualizes the spectrogram.In the amplitude vs. time plot, the x-axis represents time in nanoseconds, and the y-axis represents the amplitude. This plot provides insights into how the amplitude changes over time.The instantaneous frequency vs. time plot illustrates how the frequency changes over time. The x-axis represents time in nanoseconds, while the y-axis represents the instantaneous frequency. This plot helps in understanding the frequency dynamics of the signal.Lastly, the spectrogram plot visualizes the time-frequency content of the signal. It uses a spectrogram function with specific parameters to generate a spectrogram plot with time on the x-axis, instantaneous frequency on the y-axis, and amplitude represented by color intensity. Feel free to customize the code snippet based on your preferences. Please let me know if you have any further questions.
Answers (0)
Categories
Find more on Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!