I have the following signal on the image above:
I want to delete all the waves of small amplitudes (under 0.1) and retrace the signal so I can have this figure

3 commentaires

Jeffrey Clark
Jeffrey Clark le 30 Juin 2022
@Abdelhakim Souidi , you could use Find local maxima - MATLAB findpeaks (mathworks.com) with appropriate settings to identify all local peaks and then just ones above 0.1 then zero the amplitudes between the X midpoints of the high and low peak points that include the low peaks. If you dont have the Signal Processing Toolbox a more work is needed to use the find function and others to issolate the actual few high and low peaks before setting amplitudes to zero.
Image Analyst
Image Analyst le 30 Juin 2022
@Jeffrey Clark, your Comment actually looks like an "Answer" to me, not a Comment. Can you therefore move it down to the official "Answers" section? This "Comments" section, where you posted, is supposed to be where people ask the original poster to clarify the wording of the question, attach data, etc. Answers go below in the Answers section. A benefit is that down there (unlike here) you can earn "reputation points" if the original poster Accepts your Answer or if anyone clicks the "Vote" icon. Thanks in advance. 🙂
% signal
load('signal.mat');
%time array
load('time.mat');

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 1 Juil 2022
If you have the Image Processing Toolbox (used to find the center of the "silent" parts), try this:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s1 = load('signal.mat')
s2 = load('time.mat')
y = s1.signal;
t = s2.time; % Don't use time as the name of a variable since it's a built-in function.
subplot(3, 1, 1);
plot(t, y, 'b-');
grid on;
[upper, lower] = envelope(y, 30, 'peak');
hold on;
plot(t, upper, 'r-', 'LineWidth', 2);
threshold = 0.01;
mask = upper < threshold;
% Throw out any sections narrower than, say, 50 elements.
mask = bwareafilt(mask, [50, inf]);
yline(threshold, 'Color', 'g', 'LineWidth', 2)
y(mask) = 0;
title('Signal plus envelopes and threshold', 'FontSize', fontSize)
subplot(3, 1, 2);
plot(t, mask, 'g-', 'LineWidth', 4)
grid on;
ylim([0, 1.5]);
title('Mask indicating where no signals are', 'FontSize', fontSize)
% Find the centroid (half way point between the pulses and the peak intensities
% because we want to erase the shorter pulses between those half way points.
propsFlat = regionprops(mask, 'Centroid');
xy = vertcat(propsFlat.Centroid);
midPoints = round([1; xy(:, 1); numel(mask)])
% Now scan across each pulse section and if the max value
% in that section is less than 0.5 erase the whole section.
yMasked = y; % Initialize.
for k = 1 : length(midPoints) - 1
index1 = midPoints(k);
index2 = midPoints(k+1);
if max(y(index1 : index2)) <= 0.5
yMasked(index1 : index2) = 0; % Erase this section.
end
end
subplot(3, 1, 3);
plot(t, yMasked, 'b-', 'LineWidth', 1)
grid on;
title('Masked Signals', 'FontSize', fontSize)

3 commentaires

Abdelhakim Souidi
Abdelhakim Souidi le 1 Juil 2022
@Image Analyst Oh my god thank you! you are brilliant
Abdelhakim Souidi
Abdelhakim Souidi le 1 Juil 2022
@Image Analyst Last question if possible, if I want to do the inverse (focusing on the small amplitudes) how to do it ?
Change this line
if max(y(index1 : index2)) <= 0.5
to this line
if max(y(index1 : index2)) >= 0.5

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 30 Juin 2022
Not hard, but you forgot to attach your data. But it would go something like this
[upper, lower] = envelope(y);
mask = upper < 0.1;
y(mask) = 0;
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

1 commentaire

Abdelhakim Souidi
Abdelhakim Souidi le 1 Juil 2022
thank you so much your answer is very helpful, but some amplitude from the desired wave are distored how to overcome this issue

Connectez-vous pour commenter.

Jeffrey Clark
Jeffrey Clark le 30 Juin 2022

0 votes

@Abdelhakim Souidi , I couldn't determine if you had the data or just an image to work with. If you are working with just an image the someone with image processing could provide a more appropriate response. If you are looking to separate the signals in data you have (although they look very separate with the little information you provided) then from the information provided frequency is expected not to be a discriminator and only amplitude at the peaks are. If that is the case please refer to my original comment as one approach to your answer. Please mark as answered if you find this true.

7 commentaires

Jeffrey Clark,
Indeed, I used findpeaks function and I got the local maximas
[pks,locs, wdth] = findpeaks(sh1,ty,'MinPeakProminence',0.1,'Annotate','extents','WidthReference','halfheight');
sh1 is the envelope of my signal and ty is time axis
how to perform it from this perspective
Jeffrey Clark
Jeffrey Clark le 1 Juil 2022
Modifié(e) : Jeffrey Clark le 1 Juil 2022
@Abdelhakim Souidi ,@Image Analyst, so you know how to find the tall ones (green), do that again to find all of them (red and green), the difference being the ones to be suppressed.
Difference the locs of the ones to keep and discard to find the midpoints, then set sh1(ty>lower & ty<upper) = 0. So assuming you get pks2 and locs2 contining them all then something like this:
discard = find(~any(locs'==locs2));
% should account for the case where might be zero or more than one short peaks
for i = discard
if i == 1
sh1(ty<(locs2(i+1)+locs2(i))/2) = 0;
elseif i==length(locs2)
sh1(ty>=(locs2(i-1)+locs2(i))/2) = 0;
else
sh1(ty>=(locs2(i-1)+locs2(i))/2 & ty<(locs2(i+1)+locs2(i))/2) = 0;
end
end
Abdelhakim Souidi
Abdelhakim Souidi le 1 Juil 2022
Could I give you data to try on it
% signal
load('signal.mat');
%time array
load('time.mat');
Jeffrey Clark
Jeffrey Clark le 1 Juil 2022
@Abdelhakim Souidi , yes I kept saying midpoint but didn't when working without real data. I don't have access to findpeaks so I rolled my own, I updated the sample code above and got this with your data. Note that if the tiny signal leading into/out of the large signals is an issue you may want to zero data closer to the large peaks than midway:
Abdelhakim Souidi
Abdelhakim Souidi le 1 Juil 2022
Modifié(e) : Abdelhakim Souidi le 1 Juil 2022
@Jeffrey Clark I am confused, can you repost all the code my friend this is it
@Abdelhakim Souidi , I assume you are getting the peaks you wanted with your post:
[pks,locs, wdth] = findpeaks(sh1,ty,'MinPeakProminence',0.1,'Annotate','extents','WidthReference','halfheight');
As I said I don't have access to findpeaks but reading the documentation the parameters 'Annotate' and after only apply to when there are no returns requested and a plot is made, so only 'MinPeakProminence',0.1 affects the default behavior. In addition, the data you posted in signal.mat and time.mat doesn't match the figures you posted (compare with my plot from these files in previous post). If the MinPeakProminence you used actually found the large peaks in your plots I don't think the value would work for the data you sent, and I'm not sure what it would need to be to find both the small and large peaks without picking up peaks nearby. Perhaps you should look into using both 'MinPeakHeight' and 'MinPeakDistance' instead.
I added documentation of what the code I provided expectations are:
% locs is the rerurn from findpeaks for the time (ty) relative data for the large amplitude peaks
% locs2 is the rerurn from findpeaks for the times for large and small peaks
% sh1 is the sampled signal amplitude
% ty is the time associated with each sample amplitude
discard = find(~any(locs'==locs2));
for i = discard % clear amplitudes associated with the small peaks
if i == 1
sh1(ty<(locs2(i+1)+locs2(i))/2) = 0;
elseif i==length(locs2)
sh1(ty>=(locs2(i-1)+locs2(i))/2) = 0;
else
sh1(ty>=(locs2(i-1)+locs2(i))/2 & ty<(locs2(i+1)+locs2(i))/2) = 0;
end
end

Connectez-vous pour commenter.

Produits

Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by