How to plot a line on top of the continuous wavelet transform (CWT) output?

9 views (last 30 days)
Hello. According to the attached figure, I want to add a horizontal green line (at a scale of 60) on top of the continuous wavelet transform (CWT) output. However, the line is positioned below the CWT output. Your assistance with this request would be greatly appreciated.

Accepted Answer

dpb
dpb on 17 Jul 2024
Edited: dpb on 17 Jul 2024
data = xlsread('signal.xlsx','1');
% Extract the time and signal columns
t = data(:, 1);
signal = data(:, 2);
dt = t(3) - t(2); % sampling time
fs = 1/dt; % freq
wname = 'bior6.8';
N = length(signal);
tv = (0:N-1)/fs;
% Compute the CWT coefficients
scales = 1:200;
C = cwt(signal,scales,wname);
% Create a meshgrid fot the time and scales
[T,S] = meshgrid(tv,scales);
% Plot the CWT coefficients with time and scales
figure;
hS=surf(T,S,abs(C),'EdgeColor','none');
% leave in 3D view for now for demo purposes
%view(0,90); % Set the view to 2D
xlabel('Time (sec)');
ylabel('Scale');
xlim([0, T(end)]);
grid off
colormap (pink(240));
set(gca,'fontname','Times New Roman','FontSize',10)
hold on
SCA = 60;
% left following in temporarily for demo purposes...
plot(xlim,[1 1]*(SCA),'-r','LineWidth',2)
Illustrates the problem/issue; a 2D line parallel to the x axis with its z height equal to 0 is obscured by the surface and the color map chosen is dark enough to prevent the line from showing through. Making the surface less opaque lets one see where the line is located, but it took going to such a low value for .FaceAlpha that the surface itself is then difficult to see for the lighter shades.
So, how to fix? The problem is one needs to draw the line at or above the surface in the 3D axes space even though the desired presentation is from the Z axis direction directly towards the x,y plane. Hence, yline or its workalikes aren't the correct tool for the job, we need plot3 instead.
Z=max(abs(C),[],'all'); % max Z so can be at or above any point in surface
hL3=plot3(xlim,[SCA SCA],[Z Z],'g-','linewidth',2);
pause(10) % for demonstration -- remove when going to final use
view(0,90); % Set the view to 2D after seeing original for a while
I left in the initial line on the Z==0 plane and commented on it and later...to illustrate, the above code as is will show the surface and then the red line in 3D view and add the green line at Z==max(C), then pause for 10 seeconds so can see the result before then switch the viewpoint to the 2D plane view at which time the green line will appear on top and obscure the red line. Remove those extra pieces to use "in anger", obviously.
Sorry it took me so long to think of the cause of the problem after which the solution is obvious...
  4 Comments
Navid
Navid on 19 Jul 2024
I want to express my gratitude once again. Thank you sincerely for your valuable time.

Sign in to comment.

More Answers (1)

dpb
dpb on 12 Jul 2024
Edited: dpb on 12 Jul 2024
From one of the examples it appears that other than (rudely) executing a clf that clears any current figure when called, it looks like cwt simply creates an ordinary axis that can be accessed in the normal HG2 ways...
load kobe
cwt(kobe,1)
hAx=gca;
hold on
yline(60,'g','linewidth',2)
Attach a minimum working example that produces your example plot above if want/need something more specific,
  9 Comments
dpb
dpb on 15 Jul 2024
Lacking real data, we'll use one of the surf examples to 'spearmint with...
[X,Y,Z] = peaks(25);
CO(:,:,1) = zeros(25); % red
CO(:,:,2) = ones(25).*linspace(0.5,0.6,25); % green
CO(:,:,3) = ones(25).*linspace(0,1,25); % blue
hS=surf(X,Y,Z,CO,'edgecolor','none');
view(0,90)
xlabel('X'),ylabel('Y')
yline(0,'r')
I thought maybe the peaks would hide the line but they didn't seem to...we simply must have the actual code.
Navid
Navid on 17 Jul 2024
Dear @dpb,
Thank you for your guidance and time. I am sharing my code for further review. Upon running the code, I noticed that the horizontal line appears behind the figure.
I sincerely appreciate your help and look forward to any input or suggestions you may have. Thank you for your time and support.
Best regards,
Navid
clc
clear
close all
data = xlsread('signal.xlsx','1');
% Extract the time and signal columns
t = data(:, 1);
signal = data(:, 2);
dt = t(3) - t(2); % sampling time
fs = 1/dt; % freq
wname = 'bior6.8';
N = length(signal);
tv = (0:N-1)/fs;
% Compute the CWT coefficients
scales = 1:200;
C = cwt(signal,scales,wname);
% Create a meshgrid fot the time and scales
[T,S] = meshgrid(tv,scales);
% Plot the CWT coefficients with time and scales
figure;
surf(T,S,abs(C),'EdgeColor','none')
view(0,90); % Set the view to 2D
xlabel('Time (sec)');
ylabel('Scale');
xlim([0, T(end)]);
grid off
colormap (pink(240));
set(gca,'fontname','Times New Roman','FontSize',10)
hold on
SCA = 60;
plot(xlim,[1 1]*(SCA),'-r','LineWidth',2)
hold off

Sign in to comment.

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!