# How to plot scalogram in time period?

61 views (last 30 days)
Jan Ali on 25 Apr 2021
Edited: Jan Ali on 3 May 2021
Hi eveyone!
I want to analyze an event in a signal in a certain time span, but using the cwt(x, fs) built in function plots in Scalogram at time axes from 0 as seen in the below plot.
How can I plot the scalogram in a time interval? I appreciate if anyone can help me with this. Wayne King on 29 Apr 2021
Hi Jan, you do not mention the release you are using, but you can put the data in a timetable with the RowTimes equal to whatever they happened to be and those should be preserved in the convenience plot.
For example:
Fs = 1e3;
t = 0:1/Fs:1;
x = cos(2*pi*32*t).*(t>=0.1 & t<0.3)+sin(2*pi*64*t).*(t>0.7);
wgnNoise = 0.05*randn(size(t));
x = x+wgnNoise;
t = t+4;
% Here make the timetable
tt = timetable(x(:),'RowTimes',seconds(t'));
cwt(tt)
Now you see that the x-axis in the plot has the correct range. Here t was a duration array, but it can also be a datetime array.
Hope that helps,
Wayne
Jan Ali on 3 May 2021
Hi Wayne,
Now the y axis scales are impaired. It does not show the actual value. For exp, the 60 Hz frequency is now measured under 10 Hz. Eeven if I change the ax.YScale = 'linear'; still no actual value is plotted. Do you have any suggestion? Wayne King on 3 May 2021
Hi Jan, can you try surf() instead of imagesc()? Alternatively, can you attach a sample time series along with the necessary information like sample rate? Note in the following example, if you put a datatip on the two localized sinusoids, the frequencies are correct.
Fs = 1e3;
t = 0:1/Fs:1;
x = cos(2*pi*32*t).*(t>=0.1 & t<0.3)+sin(2*pi*64*t).*(t>0.7);
wgnNoise = 0.05*randn(size(t));
x = x+wgnNoise;
[cfs,f] = cwt(x,1e3);
ax = newplot;
surf(ax,t,f,abs(cfs));
ax.YScale = 'log';
ax.YDir = 'normal';
axis tight
hold on
plot(ax,t,coi,'w--')
xlabel('Seconds')
ylabel('Hz')
Jan Ali on 3 May 2021
Thanks alot! Much appreciate it.
Sorry for distubing you again. I am wondering how to plot that Magnitude color bar on the right?
Additionally, do you have any suggestion on the same issue for the spectrogram using stft?
I also need the same measures for the time axis. Wayne King on 3 May 2021
Hi Jan, you can do the following:
Fs = 1e3;
t = 0:1/Fs:1;
x = cos(2*pi*32*t).*(t>=0.1 & t<0.3)+sin(2*pi*64*t).*(t>0.7);
wgnNoise = 0.05*randn(size(t));
x = x+wgnNoise;
[cfs,f] = cwt(x,1e3);
ax = newplot;
surf(ax,t,f,abs(cfs));
ax.YScale = 'log';
axis tight;
cb = colorbar(ax);
cb.Title.String = 'magnitude';
As far as the spectrogram, that does not have the same time resolution as the original data. So you'll need the times from the stft() function and then shift those by t(1)
Fs = 1e3;
t = 0:1/Fs:10;
t = t+5000; % say it starts at t=5e3 seconds
x = cos(2*pi*32*t).*(t>=5001 & t<5003)+sin(2*pi*64*t).*(t>5005);
wgnNoise = 0.05*randn(size(t));
x = x+wgnNoise;
% Get the window center times from STFT
[S,F,T] = stft(x,1e3,'FrequencyRange','on'); %with whatever other options you want
% Shift by t(1)
T = T+t(1);
ax = newplot;
view(0,90)
axis tight;
cb = colorbar(ax);
cb.Title.String = 'Power(db)';
xlabel('Seconds')
ylabel('Hz')
Jan Ali on 3 May 2021
Many thanks Wayne!
Now the coi region is disappeared. Have a clue?
I really appreciate your kind support and the time you spent. Best regards,
Jan