Categories


Zero Crossing Rate in Octave

Here is a prototype of the short time average crossing rate.  Note that the zero crossing rate near the beginning of the phrase is high where the average magnitude is low.  The combination of the zero crossing rate and average magnitude can be used in an algorithm to classify components of speech.

Average zero crossing rate plot for the phrase "Mister Meryk"

Average zero crossing rate plot for the phrase "Mister Meryk"

The octave code for the prototype zero crossing rate function is shown below:

# Create a vector of speech samples
samples = wavread("four_ways_linear.wav", [5000 6600]);

# absolute value function
function a = absv(b)
	if(b < 0)
		a = -b;
	else
		a = b;
	endif
endfunction

# sign function, octave's sign function isn't quite what we need
function y = sgn(x)
	if(x < 0)
		y = -1;
	else
		y = 1;
	endif
endfunction

# zero crossing function for a vector
function y = zc(x)
	y = 0;
	for i = 1 : (length(x) - 2)
		y += absv(sgn(x(i)) - sgn(x(i + 1)));
	endfor
endfunction

# Calculate the average zero crossing rate for a window size
# of 160 samples every 40 samples
win_lngth = 160;
rate = 40;

j = 1;
for i = 1 : (length(samples) / rate)
	if(j + win_lngth < length(samples))
		win = samples(j : j + win_lngth);
		avgzcr(i) = zc(win) / (2 * win_lngth);
		j = j + rate;
	else
		avgzcr(i) = 0;
	endif
endfor

plot(avgzcr);

Next I’ll show the complete speech classification algorithm prototype in octave.

Comments are closed.