Division of array into subarrays depending on the threshold value

Hello,
I want to divide a array of signal into sub arrays such that each sub array contains values greater than the threshold(let say 0.08 for each case) untill it encounters again an increment from the threshold.
The subarray should contain the values in between the two thresholds as shown in the figure
can someone explain of how to do it.

 Respuesta aceptada

In your duplicate thread, I explained how to do it. Threshold
nonZeroElements = signal > 0;
Then use diff to look for where the hump starts and stops.

8 comentarios

If you need it explained in extreme detail in a full blown demo, see this:
clc;
clear all;
% Create sample data for Gova.
t = 1:300;
period = 40;
y = sin(2*pi*t/period);
y = max(0, y)
% Prefix a 0 so we can find the first element.
% Append a 0 so we can find the last element.
y = [0, y, 0];
subplot(2, 1, 1);
plot(y, 'bo-');
grid on;
title('Original Signal', 'FontSize', 24);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Find out where the signal is not zero.
nonZeroElements = y > 0; % Logical vector of 0's and 1's
% Find out indexes where it goes from 0 to 1 or 1 to 0.
diffPattern = diff(nonZeroElements);
% Plot it.
subplot(2, 1, 2);
plot(diffPattern, 'bo-');
grid on;
title('Difference Signal', 'FontSize', 24);
% Find out where every hump starts and stops.
humpStarts = strfind(diffPattern, [1, 0])+1
humpEnds = strfind(diffPattern, [-1, 0])
% Loop through all humps extracting each segment into a cell
for hump = 1 : length(humpEnds)
indexRange = humpStarts(hump) : humpEnds(hump);
ca{hump} = y(indexRange);
end
% Display the cell array
celldisp(ca)
Thanks for giving time to make this.
But can I know how to get the values between two humps I mean the value at which the hump starts till the start of the next hump
I tried to check for every 50 values as
k=0;
for i=1:50:length(y)-260
x1=y(1,i:i+49);
nonZeroElements = x1 > 0;
diffPattern = diff(nonZeroElements);
subplot(2, 1, 2);plot(diffPattern,'-k');hold on;
grid on;
d1=diffPattern>0;%Only need 0 to 1 trasition but not 1 to 0
subplot(2, 1, 2);plot(d1,'-r');
Needed(k)=values between the two humps if two humps exists in 50 values
k++;
end;
I am confused of how to save the values between two consecutive humps found in 50 values in a different arrays such that each array contains only the set of values between two consecutive humps as shown in the image
can I know to modify your demo as shown in the above image.
Image Analyst
Image Analyst el 11 de En. de 2014
Editada: Image Analyst el 11 de En. de 2014
You said "I want to divide a array of signal into sub arrays" and "The subarray should contain the values in between the two thresholds" so I spent a lot of time to create code that did that for you, only to have you say "I know how to get the values between two humps". Now you're asking for " the value at which the hump starts till the start of the next hump", which is given by the variable humpStarts in my code . So I don't know why you're asking for something I already gave you.
I'm very reluctant to write any more simple code for you because it really is so simple that you should be able to do it yourself, and if I do, you might just again say you already know how to do it. It's not rocket surgery - I'm sure you can handle it.
Alright. I modified the last few lines for you to go from hump start to hump start instead of hump start to hump end. Here are the lines:
% Find out where every hump starts and stops.
humpStarts = strfind(diffPattern, [1, 0])+1
% humpEnds = strfind(diffPattern, [-1, 0]) % Commented out!
% Loop through all humps extracting each segment into a cell
for hump = 1 : length(humpStarts)-1
indexRange = humpStarts(hump) : (humpStarts(hump+1) - 1);
ca{hump} = y(indexRange);
end
That should solve it for you, right?
Thanks.I tried alot to divide the original signal into sub signal such that each divided array contains the values of signal(from the start of one hump to next hump) looking for a signal in every 50 values of the original signal as shown below
clc;
clear all;
t = 1:300;
period = 40;
y = sin(2*pi*t/period);
y = max(0, y)
y = [0, y, 0];
k=0;a=1;h=1;j=1;
for i=1:50:length(y)-50
if(i<50)
x1=y(1,i:i+49);
input(1,i:i+49)=y(1,i:i+49);
nonZeroElements = x1 > 0;
diffPattern = diff(nonZeroElements);
humpStarts = strfind(diffPattern, [1, 0]);
if (length(humpStarts)>1)
for p=1:1:length(humpStarts)-1;
k=humpStarts(p);
for m=k:1:humpStarts(p+1);
needed{a}(1,m)=x1(m);
end;
check{j}=p;
j=j+1;
a=a+1;
end;
next(1,1:length(x1)-(m))=x1(1,m+1:length(x1));
elseif(length(humpStarts)==1)
m=humpStarts(1);
next(1,1:length(x1)-(m))=x1(1,m+1:length(x1));
else
next(1,humpStarts(1):length(x1))=x1(1,humpStarts(1):length(x1));
end;
else
x1=y(1,i:i+49);
input(1,i:i+49)=y(1,i:i+49);
nonZeroElements = x1 > 0;
diffPattern = diff(nonZeroElements);
humpStarts = strfind(diffPattern, [1, 0]);
if (length(humpStarts)>1)
needed{a}(1,1:length(next))=next(1,1:length(next));
needed{a}(1,length(next)+1:(length(next)+humpStarts(1)))
=x1(1,1:humpStarts(1));
a=a+1;
needed{a}(1,1:(humpStarts(2)-humpStarts(1)))=x1(1,humpStarts(1)+1:humpStarts(2));
a=a+1;
next(1,1:length(x1)-humpStarts(2))=x1(1,humpStarts(2)+1:length(x1));
elseif(length(humpStarts)==1)
needed{a}(1,1:length(next))=next(1,1:length(next));
needed{a}(1,length(next)+1:(length(next)+humpStarts(1)))
=x1(1,1:humpStarts(1));
a=a+1;
next(1,1:length(x1)-(humpStarts(1)))
=x1(1,humpStarts(1)+1:length(x1));
else
next(1,1:length(x1))=x1(1,1:length(x1));
end;
end;
end;
%--for plotting the divided signals in the needed cell array---%
e=1;
for t=1:1:length(needed)
for k=1:1:length(needed{t})
r(1,e)=needed{t}(1,k);
e=e+1;
end;
end;
plot(y);hold on;plot(r,'-r');
It is working fine except the last divided signal as shown here
I tried to figure out the problem but was not succeded.
can you try to help me out in finding the problem of why the last signal divided is not perfect which should contain the values from start of one hump to start of next humpwith but it is not.
I am trying out but unable to solve this puzzle.can someone help me out in solving the problem.
thanks.
Revert to my code. Whatever you did complicated it and broke it.
Gova ReDDy
Gova ReDDy el 14 de En. de 2014
Editada: Gova ReDDy el 14 de En. de 2014
Thanks for your patience, for the replies and guidance so far.
Yes,I was able to find out the problem and it is with the array next saving the remaining values after the two humps.

Iniciar sesión para comentar.

Categorías

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

Productos

Etiquetas

Preguntada:

el 11 de En. de 2014

Editada:

el 14 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by