Zero crossing in audio signal signal processing

10 views (last 30 days)
I am trying to work out the zero crossing on a short audio sample of some random tom toms. I've written this script in order to capture the zero crossings:
% Zero Crossing Script
x = wavread('toms.wav');
plot(x)
hold on
for i = 1:length(x)
if(x == 0)
y(i) = x;
plot(y(i), 'ro')
else
y(i) = [];
end
end
After running the script, I seem to be getting an error:
Index of element to remove exceeds matrix dimensions.
Error in zeroCrossing (line 16)
y(i) = [];
I was hoping the line that gives the error would create an empty cell when the value was other than zero. This way I could overlay exactly where the zero's occur.
Thanks in advance.

Answers (1)

Wayne King
Wayne King on 13 Apr 2013
I don't think this is the way to do zero crossing, but at any rate, you can use NaN in the above to do the plotting you are trying to do. And you don't need a for loop. Look at this example.
x = randi([-3 3],50,1);
y = NaN(size(x));
y(x==0) = 0;
plot(x,'b'); hold on;
plot(y,'ro','markerfacecolor',[1 0 0])
  2 Comments
Darryl
Darryl on 13 Apr 2013
Thanks for the help. I tried replacing y(i) = []; with y(i) = NaN, but I just end up with a row vector full of NaN's and no zeros. I've noticed that 'x' (my wav read) is in fact a column vector, so maybe that's why I'm getting the results I am.
Wayne King
Wayne King on 13 Apr 2013
Edited: Wayne King on 13 Apr 2013
I think the problem may be that testing for actual equality to 0 is not going to work with floating point numbers. That's why I suggested that was not the way to implement a zero-crossing detector.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!