How to limit data to values below threshold and have the program return what those are then graph it?

96 views (last 30 days)
I am trying to reduce the amount of excess noise in an EEG recording. I want to set a threshold and only accept values above that threshold, and then have the computer give me the values that cross this point. I've tried setting up a for loop to compare all of the values in the vector against my threshold value. If a value is above the threshold, it's marked, and its location in the vector, into a new matrix. However, I am having a difficult time with it and need some assistance.
My attempts:
for i=0:Time(78);
this = AD001; thresh_min = -50;
if (this <= thresh_min); this = thresh_minpos;
then disp(thresh_minpos);
end
plot(Time, this);
end

Answers (2)

Chad Greene
Chad Greene on 6 Oct 2014
Loops are slow and clunky. Try to use logical statements whenever possible. Run this example:
x = linspace(0,5*pi,100);
y = 10*sin(x) + randi(50,1,100);
plot(x,y,'bx')
threshold = 25;
y2 = y;
y2(y2<threshold)=NaN;
hold on
plot(x,y2,'ro')
For your case, you could use
ind = this >= thresh_min;
to return an array the same size as this. That array will have zeros and ones to indicate whether values are above the threshold or not. To get indices of this and forget about the values that don't meet the criteria, you could use
ind = find(this >= thresh_min);

dpb
dpb on 6 Oct 2014
If AD001 is the original signal, then try
AD001(AD001<=thresh_men)=nan;
This will replace the values below the threshold w/ NaN which when you plot will not be plotted but will leave the vector the same length and when plotted show empty space on the trace but keep the time spacing unaltered (which I'd think for an EEG would be significant).
If the timing is not important and you really want a reduced-length vector then save the index vector and decimate both the time vector and the original...
idx=(AD001<=thresh_men); % logical array
ECGred=AD001(idx);
t=Time(idx);
Look up in the "Language Fundamentals" chapter "Using logicals in Array Indexing" for the gory details of how the above works.

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!