How to change the spectrogram magnitude to linear?
조회 수: 14 (최근 30일)
이전 댓글 표시
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
답변 (2개)
sai charan sampara
2024년 6월 13일
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
2024년 6월 13일
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
Kalasagarreddi Kottakota
2024년 6월 13일
편집: Kalasagarreddi Kottakota
2024년 6월 13일
Star Strider
2024년 6월 13일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Time-Frequency Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!