How can I plot frequency vs magnitude of the scalogram?. The input file is the M*N matrix that is a scalogram. Any one help me by providing MATLAB code.

7 views (last 30 days)

Answers (1)

Mathieu NOE
Mathieu NOE on 6 Dec 2022
hello
the spectrum is either the mean or the peak value along the x direction of S
see code example below
you can change my home made spectrogram function to the already available functions (specgram or spectrogram) if you prefer ; that was for someone else wanting to see how a spectrogram is made
see the RHS plot made as the time averaged of the dB spectrogram
fs=2000; % Sampling rate
N=5000; % Number of data points
t=[0:1:N-1]/fs;
y=sin(250*2*pi*t)+chirp(t,100,2,500,'quadratic');
nfft = 200;
overlap = 0.75;
spectrogram_dB_scale = 80; % display this dB range on y axis
[S,F,T] = myspecgram(y, fs, nfft, overlap); % overlap is 75% of nfft here
sg_dB = 20*log10(abs(S)); % expressed now in dB
% saturation of the dB range : the lowest displayed level is spectrogram_dB_scale dB below the max level
min_disp_dB = round(max(max(sg_dB))) - spectrogram_dB_scale;
sg_dB(sg_dB<min_disp_dB) = min_disp_dB;
% plot
figure(1);
subplot(121),
imagesc(T,F,sg_dB)
hcb = colorbar('vert');
set(get(hcb,'Title'),'String','dB')
set(gca,'YDir','Normal')
xlabel('Time (secs)')
ylabel('Freq (Hz)')
title('Specgram')
colormap('jet');
subplot(122),
sdB = mean(sg_dB,2); % spectrum time averaged = mean along dim 2
plot(sdB,F)
xlabel('Mean Amplitude (dB)')
ylabel('Freq (Hz)')
title('Spectrum')
function [fft_specgram,freq_vector,time] = myspecgram(signal, Fs, nfft, Overlap)
% FFT peak spectrogram of signal (example sinus amplitude 1 = 0 dB after fft).
% signal - input signal,
% Fs - Sampling frequency (Hz).
% nfft - FFT window size
% Overlap - buffer overlap % (between 0 and 0.95)
signal = signal(:);
samples = length(signal);
% fill signal with zeros if its length is lower than nfft
if samples<nfft
s_tmp = zeros(nfft,1);
s_tmp((1:samples),:) = signal;
signal = s_tmp;
samples = nfft;
end
% window : hanning
window = hanning(nfft);
window = window(:);
% compute fft with overlap
offset = fix((1-Overlap)*nfft);
spectnum = 1+ fix((samples-nfft)/offset); % Number of windows
% % for info is equivalent to :
% noverlap = Overlap*nfft;
% spectnum = fix((samples-noverlap)/(nfft-noverlap)); % Number of windows
% main loop
fft_specgram = [];
for ci=1:spectnum
start = (ci-1)*offset;
sw = signal((1+start):(start+nfft)).*window;
fft_specgram = [fft_specgram abs(fft(sw))*4/nfft]; % X=fft(x.*hanning(N))*4/N; % hanning only
end
% one sidded fft spectrum % Select first half
if rem(nfft,2) % nfft odd
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
fft_specgram = fft_specgram(select,:);
freq_vector = (select - 1)*Fs/nfft;
% time vector
% time stamps are defined in the middle of the buffer
time = ((0:spectnum-1)*offset + round(nfft/2))/Fs;
end
  5 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!