J_code
J_code

Reputation: 347

Zero crossings around mean

I am working on developing a suite classifiers for EEG signals and I will be needing a zero-crossings around mean function, defined in the following manner:

enter image description here

Ideally if I have some vector with a range of values representing a sinusoid or any time varying signal, I will want to return a vector of Booleans of the same size as the vector saying if that particular value is a mean crossing. I have the following Matlab implementation:

ZX = @(x) sum(((x - mean(x)>0) & (x - mean(x)<0)) | ((x - mean(x)<0) & (x - mean(x)>0)));

Testing it on toy data:

[0 4 -6 9 -20 -5]

Yields:

0

EDIT: Yet I believe it should return:

3

What am I missing here?

Upvotes: 1

Views: 203

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 102261

A simple solution is to use movprod, and count the products which are negative, i.e.,

cnt = sum(sign(movprod(x-mean(x),2))<0);

With your toy example, you will get cnt = 3.

Upvotes: 0

anon
anon

Reputation:

You can use the findpeaks function on -abs(x), where x is your original data, to find the peak locations. This would give you the zero crossings in general for continuous signals which do not have zero as an actual maximum of the signal.

t = 0:0.01:10;
x = sin(pi*t);
plot(t,x)
grid

y = -abs(x);
[P,L] = findpeaks(y,t);

hold on
plot(L,P,'*')

enter image description here

Upvotes: 0

bg2b
bg2b

Reputation: 1979

An expression like:

((x-m)>0) & ((x-m)<0)

is always going to return a vector of all zeros because no individual element of x is both greater and less than zero. You need to take into account the subscripts on the xs in the definition of ZX:

((x(1:end-1)-m)>0) & ((x(2:end)-m)<0)

Upvotes: 1

Related Questions