MATLAB: Zero Crossing Rate plotting

zcrzero cross rate

I am writing a code to detect voiced vs un-voiced segment in a signal. I believe I have the correct approach and code. However, I am having difficulty trying to overlay the original signal and the zero crossing rate in the same plot. I need this to test, how effective the zero crossing rate is.
Additionally, is there any normalization that I may be missing ?
% Algorithm to identify presence of voice activity using zero crossing rate and energy
% of speech signal
clear all; clc; close all;
%% Read the input signal
[y, fs] = audioread('ISTS-16s-44100.wav');
N = length(y);
t = (0:N-1)/fs;
framelen = 128;
numframes = floor(N/framelen);
overlap = 0;
zcr = [];
for k = 1:numframes
arry = [];
zcr_frame = [];
frame = y((k-1)*framelen+1 : framelen*k) ;
for i = 2:length(frame)
arry(i) = sgn(frame(i)) – sgn(frame(i-1));
end
zcr_frame = sum(abs(arry));
zcr = [zcr zcr_frame] ;
end
figure (1); clf; hold on;
grid on;
plot(t,y);
xlabel('time(secs)'); ylabel('linear output')
t_zcr = (0:framelen:N-framelen)/fs ;
plot(t_zcr,zcr);

Best Answer

hello again
this is an improved code - no inner for loop needed
clear all; clc; close all;
%% Read the input signal
[y, fs] = audioread('test_voice.wav');
N = length(y);
t = (0:N-1)/fs;
framelen = 128;
numframes = floor(N/framelen);
overlap = 0;
zcr = [];
for k = 1:numframes
frame = y((k-1)*framelen+1 : framelen*k) ;
arry = sign(frame(2:framelen)) - sign(frame(1:framelen-1));
zcr_frame = sum(abs(arry));
zcr = [zcr; zcr_frame] ;
end
% normalisation
zcr = zcr./max(zcr);
y = y./max(abs(y));
figure (1);
% t_zcr = (0:framelen:N-framelen)/fs ; % time stamp positionned at beginning of frame
t_zcr = (framelen/2:framelen:N-framelen/2)/fs ; % time stamp positionned at center of frame
plot(t,y,'b',t_zcr,zcr -1.5,'r');grid on;
xlabel('time(secs)'); ylabel('linear output')