Filter löschen
Filter löschen

How to change the spectrogram magnitude to linear?

8 Ansichten (letzte 30 Tage)
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota am 13 Jun. 2024
Kommentiert: Star Strider am 13 Jun. 2024
I have the following code of 100 Hz tone with a magnitude of 2. But on the colorbar I get magnitude as 110. Could you help me to resolve this?
clear all; clc;
% Sample signal
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:2; % Time vector
x = 2*cos(2*pi*100*t); % Example signal
% Compute the spectrogram
window = hann(256); % Window function
noverlap = 128; % Number of overlapping samples
nfft = 256; % Number of FFT points
[S, F, T] = spectrogram(x, window, noverlap, nfft, fs);
S_magnitude = abs(S); % Magnitude of the spectrogram
% Plot the spectrogram
figure;
imagesc(T, F, S_magnitude);
axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Spectrogram (Linear Scale)');
colorbar;

Antworten (2)

sai charan sampara
sai charan sampara am 13 Jun. 2024
Hello,
The "spectrogram" function in MATLAB returns the Short-Time Fourier Transform(STFT) of the input signal. It is used to analyze how the frequency content of a signal changes over time. It gives the variation of signal in the frequency domain over time. In this case, since the signal is a constant frequency signal the spectrogram is a straight line with the maximum value at 100 Hz. Which is the frequency of the signal "x". The "colorbar" varies up to 110 because the maximum value of "S_magnitude" is around 110. As shown in the code below the maximum value of "S_magnitude" for different time values is 114.97 and from the "F" vector we can see that it occurs at around 100Hz frequency.
clear all; clc;
% Sample signal
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:2; % Time vector
x = 2*cos(2*pi*100*t); % Example signal
% Compute the spectrogram
window = hann(256); % Window function
noverlap = 128; % Number of overlapping samples
nfft = 256; % Number of FFT points
[S, F, T] = spectrogram(x, window, noverlap, nfft, fs);
S_magnitude = abs(S); % Magnitude of the spectrogram
% Plot the spectrogram
figure;
imagesc(T, F, S_magnitude);
axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Spectrogram (Linear Scale)');
colorbar;
[val,idx]=max(S_magnitude)
val = 1x14
114.9696 114.9701 114.9698 114.9698 114.9701 114.9696 114.9701 114.9698 114.9698 114.9701 114.9696 114.9701 114.9698 114.9698
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = 1x14
27 27 27 27 27 27 27 27 27 27 27 27 27 27
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
F(idx(1))
ans = 101.5625
  1 Kommentar
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota am 13 Jun. 2024
Hi @sai charan sampara, the problem is about the wrong magnitude it is showing. When you apply an stft, the magnitude of the color bar should be the strength of the signal. Here the strength of the signal is 2 units. So, I expect the colorbar to stand maximum at 2 units. But it is 115. Dont know what exactly the output of spectrogram function.

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 13 Jun. 2024
I susp[ect that you may actually want to use the pspectrum function with the 'spectrogram' type. To understand the differences between them, see the spectrogram documentation section on Compare spectrogram and pspectrum Functions. (The principal differences are the units of the output.)
I changed the output plot to surfc to make this a bit more understandable. Change it back if you like, or use
view(0,90)
with the surfc plot.
Try this —
clear all; clc;
% Sample signal
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:2; % Time vector
x = 2*cos(2*pi*100*t); % Example signal
% Compute the spectrogram
window = hann(256); % Window function
noverlap = 128; % Number of overlapping samples
nfft = 256; % Number of FFT points
% [S, F, T] = spectrogram(x, window, noverlap, nfft, fs);
[S, F, T] = pspectrum(x, fs, 'spectrogram', 'OverlapPercent',50);
S_magnitude = abs(S); % Magnitude of the spectrogram
% Plot the spectrogram
figure;
% imagesc(T, F, S_magnitude);
surfc(T, F, S_magnitude, 'EdgeColor','interp')
% axis xy;
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Spectrogram (Linear Scale)');
colorbar;
zlim([0 2])
% view(0,90)
.
  2 Kommentare
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota am 13 Jun. 2024
Bearbeitet: Kalasagarreddi Kottakota am 13 Jun. 2024
Hi @Star Strider, I changed the magnitude of signal x to 5 and expecting the same in the spectrogram, but it turned to max as 12. I am wondering why this change. I am basically looking for STFT of the signal whose color bar directly shows the same magnitude of the signal.
clear all ; clc;
% Sample signal
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:2; % Time vector
x = 5*cos(2*pi*100*t); % Example signal%-----------------------chnage
% Compute the spectrogram
window = hann(256); %Window function
noverlap = 128; % Number of overlapping samples
nfft = 256; % Number of FFT points
% [S, F, T] = spectrogram(x, window, noverlap, nfft, fs);
[S, F, T] = pspectrum(x, fs, 'spectrogram' , 'OverlapPercent' ,50);
S_magnitude = abs(S); % Magnitude of the spectrogram
% Plot the spectrogram
figure;
% imagesc(T, F, S_magnitude);
surfc(T, F, S_magnitude, 'EdgeColor' , 'interp' )
% axis xy;
xlabel( 'Time(s)' );
ylabel( 'Frequency(Hz)' );
title( 'Spectrogram (Linear Scale)' );
colorbar;
% zlim([0 2])
Star Strider
Star Strider am 13 Jun. 2024
I read through the documentation, including for the periodogram function. It seems that it should produce the power spectrum, not the power spectral density, however taking the square root of ‘S’ still does not give the desired result. Maybe one of the MATLAB staff can explain this. I’m obviously overlooking something.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by