Help with breaking into windows based on a condition??

1 view (last 30 days)
Hi, I have a array of numbers (0,1) which is attached in the CSV file. I want to break them into windows where the condition meets(1) for greater than or equal to 30 seconds.
If it is for only 10 seconds continuous it shouldn't be considered a window. I also want the locations for the start and stop of the windows.

Accepted Answer

Image Analyst
Image Analyst on 15 Jan 2017
Assuming the spacing between elements if 1 second, you can use regionprops() to measure the starting and stopping indexes of each region:
% Read data file.
data = fileread('nte.csv');
% Skip 'condition' and convert from characters to numbers.
data = sscanf(data(10:end), '%d')
% Get rid of regions fewer than 30 elements
data = bwareaopen(data, 30);
% Assign an ID number to each contiguous region of 1's.
[labeledRegions, numRegions] = bwlabel(data);
% Find starting and stopping indexes, plus area, for each region.
props = regionprops(labeledRegions, 'PixelList', 'Area');
% props is a structure array.
% Get all the areas into single arrays for convenience.
allAreas = [props.Area]
for k = 1 : numRegions
starts(k) = props(k).PixelList(1, 2);
stops(k) = props(k).PixelList(end, 2);
end

More Answers (2)

John BG
John BG on 15 Jan 2017
Hi Satya
convolution is very useful to answer your question
1. capturing data and display
A=importdata('NTE.csv')
L=A.data';
figure(1);stem(L);
.
2. generating window of 30 samples
mask=ones(1,30)
3. convolution
D=conv(L,mask);
4. spot windows of length 30 consecutive ones.
d_start= find(D==30)-30;
L2=zeros(1,length(L))
L2(d_start)=1
RESULT:
L2 contains the positions of the signal where a window of 30 or more ones start.
A burst of for instance 5 ones in L2 means there is a window of 35 samples long.
Windows below 30 consecutive samples are not marked
5. Display
figure(2);stem(L);
hold all;
figure(2);stem(L2,'r');
.
Satya
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

Walter Roberson
Walter Roberson on 15 Jan 2017
data = csvread('nte.csv');
Fs = 1; %sampling rate in hz
Samp30 = floor(Fs * 30); %samples in 30 seconds
template = ones(1, Samp30) ;
starts = strfind([0,data], [0,template]);
ends = strfind([data, 0],[template, 0]) + Samp30 - 1;
Now starts and ends are vectors in which starts(k) gives the position of the beginning of a stretch that is long enough, and ends(k) gives the position of the last place that is set for the run.
  4 Comments
Image Analyst
Image Analyst on 16 Jan 2017
That's good to know. Though it would be nice if they also allowed the 'HeaderLines' option to be consistent with all the other functions that use that method of specifying the number of lines to skip.
It's not clear that specifying the row and column to start extracting data will work now since they had that before, which worked if you had strictly numerical data, but barfed if you had any string header lines. So now it works, but those of us remembering the old way may still not know to try it again.
Walter Roberson
Walter Roberson on 16 Jan 2017
Yeh, what happened was that they quietly removed the numeric-only restriction from the documentation without a release note. If I recall correctly, string row headers are permitted now too. This still relies upon undocumented behaviour of textscan().
readtable() appears to use a different front end parser to figure out what columns mean. On the other hand readtable() documents (or at least implies) that it calls textscan(). (Well, not for .xlsx files certainly.)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!