How to plot scalogram in time period?
35 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
4 Comments
More Answers (2)
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));
view(0,90); shading interp;
ax.YScale = 'log';
ax.YDir = 'normal';
axis tight
hold on
plot(ax,t,coi,'w--')
xlabel('Seconds')
ylabel('Hz')
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';
view(0,90); shading interp;
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;
surf(T,F,20*log10(abs(S))); shading interp;
view(0,90)
axis tight;
cb = colorbar(ax);
cb.Title.String = 'Power(db)';
xlabel('Seconds')
ylabel('Hz')
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!