Help with breaking into windows based on a condition??
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Image Analyst
2017-1-15
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
0 个评论
更多回答(2 个)
John BG
2017-1-15
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176655/image.jpeg)
.
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176657/image.jpeg)
.
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
0 个评论
Walter Roberson
2017-1-15
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 个评论
Image Analyst
2017-1-16
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
2017-1-16
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.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!