Closest index in the annotation of a signal

Hello everyone!I have a problem with the annotation of this wave. I need to annotate the points in red , which correspond to the index locs, but only the previous one and the after one with respect the point in black which correspond to the index locs_zero. In this way I don’t annotate the another points in red. Locs and locs_zero have two different dimensions.

5 comentarios

Les Beckham
Les Beckham el 12 de Jul. de 2022
You need to post your plot and the code you used to add the annotations if you wish to get assistance. Please also explain more clearly what you expect to see and how what is actually happening differs from what you want.
Lucrezia Mattia
Lucrezia Mattia el 12 de Jul. de 2022
Editada: dpb el 12 de Jul. de 2022
Sure, the code is this: clear all
clc
close all
load('signal')
start_time = 3055;
end_time = 3058;
fs = 1000;
tempo=[0:0.001:size(data,1)*0.001-0.001];
tempo=tempo(start_time*fs:end_time*fs);
%time-axis
window = 1800;
%Start time and End time in seconds to cancel the first seconds
x=data(:,6);
x = x(start_time*fs:end_time*fs);
trigger = data(:,4);
trigger = trigger(start_time*fs:end_time*fs);
[b,a] = butter(4,50/(fs/2),'low');%ButterWorth low pass filter of fouth order, cut off frequency of 50 Hz
y=filter(b,a,x);
y_filtered = ssf(y);%I apply the function that create a vector in which i sum the derivative when is positive, the peak of the positive wave corresponds to the maximal slope in my original signal-->Onset;
%I need to choose a threshold, to do it i create a mobile window and i
%iapply it on the filtered signal, the window is long n.samples == Window;
M = movmax(y_filtered,window);
m = movmin(y_filtered,window);
threshold = (2*M+m)/3;%Mean pressure or PAM
segnale_pulito = y_filtered>threshold; % I obtain a logic vector in which i have the portion of the filtered signal that is above the threshold
picchi = segnale_pulito.*y_filtered; %I multiply the logical vector for the values of the filtered signal;
[pks,locs] = findpeaks(picchi);
plot(tempo,y,tempo(locs),y(locs),'o');
figure
plot(tempo,y_filtered,tempo(locs),y_filtered(locs),'o');
%I find the peaks on y-axis;
%I have to find the points in which i have the correct onset and the
%correct peak; The idea: I find the points in which the derivative changes
%its sign, which correspond to the points of zero-crossing
z = diff(y);
figure
plot(tempo(1:end-1),z)
hold on
plot(tempo(locs),z(locs),'o');
figure
plot(tempo,y,tempo(locs),y(locs),'ob');
hold on
plot(tempo(1:end-1),z,'k');
plot(tempo(locs),z(locs),'or');
%Derivative of the original signal (not filtered);
%I want to store the indexes in which i have the zero-crossing,where the
%function changes the sign.
locs_zero = []; % I create an empty vector
for k = 1:(length(z)-1)
if sign(z(k))~= sign(z(k+1))
locs_zero = [locs_zero k];
end
end
figure
plot(tempo,y,tempo(locs_zero),y(locs_zero),'o');
hold on
plot(tempo(locs),y(locs),'ok')
Lucrezia Mattia
Lucrezia Mattia el 12 de Jul. de 2022
I have uploaded the image obtained at the end
Lucrezia Mattia
Lucrezia Mattia el 12 de Jul. de 2022
Lucrezia Mattia
Lucrezia Mattia el 12 de Jul. de 2022
i want to obtain something like this using the onset

Iniciar sesión para comentar.

Respuestas (1)

Hi Lucrezia Mattia,
The key point here is to find the adjacent elements of the indices marked with black coloured circles. Let us define a function “findAdjacents” that takes input the index from “locs” array, and the locs_zero array. This function finds the indices of the left and right red circles to the index passed as an argument. Using this function, we can obtain the indices of all those red circles that we want to annotate.
Here is the code for the “findAdjacents” function.
function result = findAdjacents(index, target)
left = -1;
right = -1;
% Loop over the target array
for i=1:length(target)
% Find the value stored at current index i
redindex = target(i);
% If the redindex value is less than index, then it can be
% one of the red circles that is present to the right of desired
% black circle. The right red circle will be the next index of
% the last left circle to the desired black colour
if redindex < index
left = redindex;
if i + 1 <= length(target)
right = target(i + 1);
end
end
end
if left ~= -1
result = [left];
end
if right ~= -1
result = [result right];
end
end
Now, since I do not have the “signal” data that you have loaded, I have taken the following data.
square = @(x) x.*x;
x = -4:4;
y = square(x);
In this example, I will consider black markers are at positions 3 and 7, the remaining all are plotted with red. Now, ideally “findAdjacents” function should return the positions, 2, 4, 6, and 8.
blackcircles = [3, 7];
redcircles = [1, 2, 4, 5, 6, 8, 9];
marked = [];
for bidx = 1:length(blackcircles)
blackindex = blackcircles(bidx);
result = findAdjacents(blackindex, redcircles);
left = result(1);
right = result(2);
marked = [marked left right];
end
disp(marked);
2 4 6 8
% code related to plotting red and black circles is not added here as it
% was already present in the question
The next objective is to annotate these points. To annotate the points in a plot, we can use the “text” function.
for idx = 1:length(marked)
index = marked(idx);
text(x(index), y(index), "point" + num2str(idx));
end
To know more about the “text” function, please refer to the following link.
I hope this answers your question.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Jul. de 2022

Respondida:

el 23 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by