Plotting spectrograph from spectrum analyzer data

I have the following data obtained the following data from a spectrum analyzer. I need some pointers on plotting it to obtain the spectrogram as shown in this image
I have attached my data in this question. The Header contains data from the instrument (such as center frequency) and the Timedata has unsigned array of 10x131071 and Value has the value at each point of time (same size)
_________________
I tried using the following but to no avail
% Convert time_data to double and calculate the sampling frequency
dt = double(time_data(1, 2) - time_data(1, 1)); % Assuming a constant time interval
fs = 1 / dt;
% Transpose the amplitude_data matrix
amplitude_data = amplitude_data';
% Number of rows in amplitude_data
num_rows = size(amplitude_data, 1);
% Plot spectrogram for each row
for row = 1:num_rows
figure;
spectrogram(amplitude_data(row, :), hamming(256), 250, 256, fs, 'yaxis');
title(['Spectrogram for Row ', num2str(row)]);
xlabel('Time');
ylabel('Frequency');
colorbar; % Display the color scale
end

1 Kommentar

Can you also include the code that loads the files please?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Yiour data needed a bit of tweaking. Tehe times in the ‘TimeData’ matrix are contiguous, so they can be converted into a vector using reshape, and then the same with the ‘Values’ matrix.
Try this —
files = dir('*.mat');
for k = 1:numel(files)
load(files(k).name)
end
% Header
TimeData = double(TimeData);
TimeV = sort(reshape(TimeData.', [], 1)); % Vector From Matrix
Ts = mean(diff(TimeV))
Ts = 2.2249e+04
fs = 1/Ts
fs = 4.4946e-05
ValuesV = reshape(Values.', [], 1); % Vector From Matrix
figure
semilogy(TimeV, ValuesV)
xlabel('Time (unit)')
ylabel('Amplitude nit)')
figure;
spectrogram(ValuesV, hamming(256), 250, 256, fs, 'yaxis');
% title(['Spectrogram for Row ', num2str(row)]);
xlabel('Time');
ylabel('Frequency');
colormap(turbo)
colorbar; % Display the color scale
[p,f,t] = pspectrum(ValuesV,fs,'spectrogram');
figure
surfc(f,t,p', 'EdgeColor','interp')
xlabel('Frequency (cycles/time unit)')
ylabel('Time (unit)')
zlabel('Magnitude')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
colorbar
figure
surfc(f,t,mag2db(p'), 'EdgeColor','interp')
xlabel('Frequency (cycles/time unit)')
ylabel('Time (unit)')
zlabel('Magnitude (dB)')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
colorbar
figure
surfc(f,t,mag2db(p'), 'EdgeColor','interp')
xlabel('Frequency (cycles/time unit)')
ylabel('Time (unit)')
zlabel('Magnitude (dB)')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
hcb = colorbar;
hcb.Label.String = 'Magnitude (dB)';
view(90,90)
I did this with both spectrogram and pspectrum (that I actually prefer fot these sorts of analyses).
.

4 Kommentare

StarStrider,
Thanks for the in-depth answer.
I forgot to add this in my original question: the data does look repeated (because the spectrum analyzer takes 5 averages).
I have attached similar data (TimeData and ValuesData) along with the actual spectrograph that I got from the spectrum analyzer--which I am trying to recreate with MATLAB
As always, my pleasure!
The original times were contiguous, and that spectrogram matched an image you originally posted (that now seems to have disappeared).
Calculating the average of the spectra and the spectrum of the averaged time-domain signals would produce approximately the same result (although I am still uncertain how to interpret the time values themseelves).
These data do not appear to be the same as the earlier set —
files = dir('*.mat');
for k = 1:numel(files)
load(files(k).name)
end
% whos
% Header
TimeData = double(TimeData);
% TimeV = sort(reshape(TimeData.', [], 1)); % Vector From Matrix
ts = mean(diff(TimeData(1,:)))
ts = 14592
fs = 1/ts
fs = 6.8531e-05
TimeV = TimeData(1,:); % Time Vector (Units = ?)
ValuesV = mean(Values); % Calculate Column Means
figure;
spectrogram(ValuesV, hamming(256), 250, 256, fs, 'yaxis');
% title(['Spectrogram for Row ', num2str(row)]);
xlabel('Time');
ylabel('Frequency');
title('spectrogram')
colormap(turbo)
colorbar; % Display the color scale
[p,f,t] = pspectrum(ValuesV,fs,'spectrogram');
figure
surfc(f,t,p', 'EdgeColor','interp')
xlabel('Frequency (cycles/time unit)')
ylabel('Time (unit)')
zlabel('Magnitude')
title('pspectrum')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
colorbar
figure
surfc(f,t,mag2db(p'), 'EdgeColor','interp')
xlabel('Frequency (cycles/time unit)')
ylabel('Time (unit)')
zlabel('Magnitude (dB)')
title('pspectrum')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
colorbar
figure
surfc(f,t,mag2db(p'), 'EdgeColor','interp')
xlabel('Frequency (cycles/time unit)')
ylabel('Time (unit)')
zlabel('Magnitude (dB)')
title('pspectrum')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
hcb = colorbar;
hcb.Label.String = 'Magnitude (dB)';
view(90,90)
figure
tiledlayout(2,1)
nexttile
surfc(f,t,mag2db(p'), 'EdgeColor','interp')
xlabel(["Frequency" "(cycles/time unit)"])
ylabel('Time (unit)')
zlabel('Magnitude (dB)')
title('pspectrum')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])
colormap(turbo)
hcb = colorbar;
hcb.Label.String = 'Magnitude (dB)';
view(90,90)
nexttile
plot(TimeV, mag2db(ValuesV))
xlabel('Time (unit)')
ylabel('Amplitude (dB)')
grid
sgtitle('Time Domain and Spectrogram (pspectrum) Plots')
Even after looking at the ‘Header’ information, I do not understand the time units or format. That affects the displayed times as well as the frequency, since the sampling frequency is based on the time vector.
I am not certain what your instrumentation is displaying. It seems to me that the frequency should be on the y-axis, as it is in the MATLAB plots.
.
Thanks again, Star Strider. You always outdo yourself!
(I think the only way to get the frequency is to do it manually--create an array with the same size as the values, starting at center freq-span/2, and ending at center freq+span/2.
Thanks again!
As always, my pleasure!
It would help to know what the actual time values are. They must correspond to something useful, however I cannot figure out how to convert them.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by