How to change the spectrogram magnitude to linear?
16 ビュー (過去 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!