clc;
clear all;
close all;
[signal,fs]=wavread('voice.wav');
signal = signal(:); %convert to column
Tw = 25; % frame duration (ms)
Ts = 10; % frame shift (ms)
Nw = round( 1E-3*Tw*fs ); % frame duration (samples)
Ns = round( 1E-3*Ts*fs ); % frame shift (samples)
L = length( signal );
% To calculate number of frames
M = floor((L-Nw)/Ns+1); %Nw=Frame length, Ns=Frame shift
%to see if the input vector can be divided into frames exactly
E = (L-((M-1)*Ns+Nw));
%To see if padding is actually needed
if( E>0 ) % Padding is needed
% how much padding will be needed to complete the last frame?
P = Nw-E;
% pad with zeros
signal = [ signal; zeros(P,1) ];
else
M = M-1;
end
%Frame indexing
indf = Ns*[ 0:(M-1) ]; % indexes for frames
inds = [ 1:Nw ]'; % indexes for samples
indexes = indf(ones(Nw,1),:) + inds(:,ones(1,M)); % combined framing indexes
frames = signal( indexes );
No_of_frames=size(frames,2);
% Zero justification
for i=1:No_of_frames
frames(:,i)=frames(:,i)-round(mean(frames(:,i)));
end
% zcr=sum(frames(1:end-1, :).*frames(2:end, :)<=0);
%
% for i=1:No_of_frames
% frame=frames(:, i)-Ns;
% zcr(i)=sum(frame(1:end-1).*frame(2:end)<=0); % signals at zero do count for zcr
% end
%
for i=2:No_of_frames
z(:,i)= 0.5*mean(abs(sign(frames(:,i)-sign(frames(:,(i-1))))));
end
% % Z=z/Nw;
zcross = sum(z, 1) / (2*Nw);
% Signdif = sign(frames(2:end))-sign(frames(1:end-1));
%
% for i=2:No_of_frames
% i_start = (n-1)*iHopLength + 1;
% i_stop = min(length(x),i_start + iBlockLength - 1);
% % compute the zero crossing rate
% vzc(n) = 0.5*mean(abs(diff(sign(x(i_start:i_stop)))));
% end