How to change the spectrogram magnitude to linear?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kalasagarreddi Kottakota
el 13 de Jun. de 2024
Comentada: Star Strider
el 13 de Jun. de 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;
0 comentarios
Respuestas (2)
sai charan sampara
el 13 de Jun. de 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)
F(idx(1))
Star Strider
el 13 de Jun. de 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 comentarios
Kalasagarreddi Kottakota
el 13 de Jun. de 2024
Editada: Kalasagarreddi Kottakota
el 13 de Jun. de 2024
Star Strider
el 13 de Jun. de 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.
Ver también
Categorías
Más información sobre Time-Frequency Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!