close all;
clear all;
clc;
% [z,Fs]=audioread('Audio.wav');
% [z,Fs]=audioread('msg.wav');
% [z,Fs]=audioread('shake.wav');
% [z,Fs]=audioread('tweet.wav');
% [z,Fs]=audioread('Global.wav');
[z,Fs] = audioread('system.wav'); %读取语音信号的数据，赋给变量z
figure(1);
subplot(3,1,1)
plot(z);
set(gca,'Fontsize',22);
title('原始语音信号','Fontsize',22);
%定义采样的点数和重复点数
m_1 = length(z);                      
chongfudianshu = 160;            %选取重复的点数为160
L_1 = 250-chongfudianshu;
%判断总共有多少个这样的段
nn_1 = (m_1-250)/L_1;
N = ceil(nn_1);
count_zeros = zeros(1,N+1);
%段内过零点统计
%thesh为一界限，不对（-thresh，thresh）之间的点经行过零点统计
% threshhold = 0.000010;
threshhold = 0.00010;
for n = 0:N-1            
    for k = L_1*n+1:(L_1*n+250)
        if z(k) > threshhold && z(k+1) < -threshhold || z(k) < -threshhold && z(k+1) >threshhold
%             count(n)=count(n)+1;
           count_zeros(n+1) = count_zeros(n+1)+1;
        end
    end
end
%最后一段零点统计
for i = k:m_1-1;
    if  z(i)>threshhold && z(i+1)<-threshhold || z(i)<-threshhold && z(i+1)>threshhold
        count_zeros(N+1) = count_zeros(N+1)+1;
    end
end
%过零点统计图
subplot(3,1,2)
plot(count_zeros);
set(gca,'Fontsize',22);
title('过零点个数统计图','Fontsize',22);
%语音信号的分段提取
%选取合适的阈值
threshold_1 = 15;
y = zeros(1,N+1);
i = 0;
for n1 = 1:N+1
    i = i+1;
    if  count_zeros(n1)>threshold_1          
        y(i) = count_zeros(n1);
    else
        y(i) = 0;
    end
end
subplot(3,1,3)
plot(y);
set(gca,'Fontsize',22);
title('选取适当阈值后的过零点个数统计图','Fontsize',22);
%提取各个语音片段
pianduan = 0;  %确定第几个片段
qidian = 0;     %分段时确定每一个片段的起点标志
for n2 = 1:N-1
    if y(n2)>0
        for i = 1:L_1
            z2((n2-1-qidian)*L_1+i) = z((n2-1)*L_1+i);
        end
        if  y(n2)>0 && y(n2+1)==0 && y(n2+2)==0
            pianduan = pianduan+1;
            qidian = n2-1;
        end
        switch pianduan   %将每一片段转换成wma格式并保存
            case 0
                audiowrite('z_0.wav',z2,Fs)
            case 1
                audiowrite('z_1.wav',z2,Fs)
            case 2
                audiowrite('z_2.wav',z2,Fs)
            case 3
                audiowrite('z_3.wav',z2,Fs)
            case 4
                audiowrite('z_4.wav',z2,Fs)
            case 5
                audiowrite('z_5.wav',z2,Fs)
            case 6
                audiowrite('z_6.wav',z2,Fs)
            case 7
                audiowrite('z_7.wav',z2,Fs)
            case 8
                audiowrite('z_8.wav',z2,Fs)
            case 9
                audiowrite('z_9.wav',z2,Fs)
            otherwise
                disp('error')
        end
        if  y(n2)>0 && y(n2+1)==0 && y(n2+2)==0
            z2 = 0;
        end        
    end
end
%选取4段分割后的语音信号作为输出
figure(2);
[z0,Fs] = audioread('z_0.wav');
subplot(2,2,1)
plot(z0);
set(gca,'Fontsize',22);
title('分割后的语音信号片段一','Fontsize',22);
xlabel('采样点','Fontsize',22);ylabel('幅值','Fontsize',22);
[z1,Fs] = audioread('z_1.wav');
subplot(2,2,2)
plot(z1);
set(gca,'Fontsize',22);
title('分割后的语音信号片段二','Fontsize',22);
xlabel('采样点','Fontsize',22);ylabel('幅值','Fontsize',22);
[z2,Fs] = audioread('z_2.wav');
subplot(2,2,3)
plot(z2);
set(gca,'Fontsize',22);
title('分割后的语音信号片段三','Fontsize',22);
xlabel('采样点','Fontsize',22);ylabel('幅值','Fontsize',22);
[z3,Fs] = audioread('z_3.wav');
subplot(2,2,4)
plot(z3);
set(gca,'Fontsize',22);
title('分割后的语音信号片段四','Fontsize',22);
xlabel('采样点','Fontsize',22);ylabel('幅值','Fontsize',22);
% dd=z1-z3;
% aa=sum(dd);
% dl=1;
