matlab处理采集的声音数据

上次采集的声音信号需要进过一系列的信号的预处理才可以用于机器学习算法的使用。

数据处理需要把时域信号转换成频域信号,并把无用频率删除了,并把对应的信号分隔成22个样本,分开样本的意思就是可以做训练集和测试机,从而检测我们训练的模型的正确性。采集的过程如下


1.将采集的信号切割到固定的大小

package com.hemin.balltest;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;

public class DeletDatato300w {

	public static void main(String[] args) throws IOException {
		String path ="H:\\数据采集\\验\\有线通道\\湿磨\\";
		String pro1 = "有线";
		//String pro1 = "无线";
		for(int i=1 ;i<103 ;i++){
			String newpath =path+ String.valueOf(i)+"\\";
			System.out.println(newpath);
			File dir = new File(newpath);
			String[] children = dir.list();
			for (String string : children) {
				if(string.contains(pro1)){
					String filepath = newpath+string;
					String wfile = "d:\\data\\有线\\"+String.valueOf(i)+"_"+string;
					File df = new File(wfile); 
					File f = new File(filepath);
					FileWriter wf = new FileWriter(wfile);
					getTotalLines(f , wf);
					wf.close();
				}	
//				if(string.contains(pro1)){
//					String filepath = newpath+string;
//					String wfile = "d:\\data\\"+String.valueOf(i)+"_"+string;
//					File df = new File(wfile); 
//					File f = new File(filepath);
//					FileWriter wf = new FileWriter(wfile);
//					getTotalLines(f , wf);
//					wf.close();
//				}	
			}
		}

	}

    public static void  getTotalLines(File file ,FileWriter wfile) throws IOException {
    	BufferedWriter wb = new BufferedWriter(wfile);
        FileReader in = new FileReader(file);
        LineNumberReader reader = new LineNumberReader(in);
        String s = reader.readLine();
        int lines = 0;
        while (s != null) {
            lines++;
            s = reader.readLine();
            if(lines>1000 && lines<=3001000 ){
            	//System.out.println(s);
            	wb.write(s);
            	wb.newLine();
            }
            wb.flush();
        }
        wb.close();
        reader.close();
        in.close();
    }
}

2.把对应的信号从文件中分离出来

%%
clc;
clear;
%%
file=dir('D:\数据\球磨机\5\无线\*.lvm');
%%遍历所有文件
for n=1:length(file)
    n
    data=importdata(['D:\数据\球磨机\5\无线\',file(n).name]);
    path_all=regexp(file(n).name,'_','split');
    dirname=path_all{1};
    a=['mkdir ' ['D:\data\通道数据\' dirname]];
    system(a) ;
   
    path1 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch1.mat'];
    path1_1 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch1.mat'];
    path2 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch2.mat'];
    path1_2 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch2.mat'];
    path3 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch3.mat'];
    path1_3 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch3.mat'];
    path4 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch4.mat'];
    path1_4 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch4.mat'];
%     path5 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch5.mat'];
%     path1_5 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch5.mat'];
%     path6 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch6.mat'];
%     path1_6 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch6.mat'];
%     path7 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch7.mat'];
%     path1_7 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch7.mat'];
%     path8 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_ch8.mat'];
%     path1_8 = ['d:\data\通道数据\',path_all{1},'\',path_all{1},'_BM_ch8.mat'];
%     

    
%     %% 保存8列
%     %1
     data1 = data(:,2);
     save(path1,'data1');
     BM_data1 = creat_BM_data(data1);
     save(path1_1,'BM_data1');
     %2
    data2 = data(:,3);
    save(path2,'data2');
    BM_data2 = creat_BM_data(data2);
     save(path1_2,'BM_data2');
     %3
    data3 = data(:,4);
    save(path3,'data3');
    BM_data3 = creat_BM_data(data3);
     save(path1_3,'BM_data3');
    %
    data4 = data(:,5);
    save(path4,'data4');
    BM_data4 = creat_BM_data(data4);
     save(path1_4,'BM_data4');
%     %
%     data5 = data(:,2);
%     save(path5,'data5');
%     BM_data5 = creat_BM_data(data5);
%      save(path1_5,'BM_data5');
%     %
%     data6 = data(:,3);
%     save(path6,'data6');
%     BM_data6 = creat_BM_data(data6);
%      save(path1_6,'BM_data6');
%      %
%     data7 = data(:,4);
%     save(path7,'data7');
%     BM_data7 = creat_BM_data(data7);
%      save(path1_7,'BM_data7');
%      %
%     data8 = data(:,5);
%     save(path8,'data8');
%     BM_data8 = creat_BM_data(data8);
%      save(path1_8,'BM_data8');
end





3.将所有的文件合并至一个文件中

%%
clc;
clear;
%%
for c=1:8
    c
        root_path = 'D:\数据\5\通道数据\';
        ch=['ch',num2str(c)];
        ch1=['ch',num2str(c)];
        ch_data = []
        all_path = [root_path , ch];
        %%
        for i = 1:102
            path = [all_path ,'/', num2str(i),'_BM_',ch1,'.mat'];
            data = importdata(path);
            ch_data(i,:,:) = data ;
        end
        sp = ['d:/data/ch',num2str(c),'.mat']
        save(sp,'ch_data')
        
end

4.用平均值降维

clc;
clear;

for i = 1:8
    i
    path = ['D:/数据0/ch_' , num2str(i)];
    spath=['D:/数5/300/ch_',num2str(i),'.mat'];
    load(path)
    ch_infor = [];
    for j = 1:102
        for k= 1:22
            data = reshape(ch_data(j,k,:) , 1,6000);
            for m=1:300
                data_new(m) = mean(data(m*20-19:m*20));
            end
            ch_infor(j,k,: )=data_new;
        end
    end
     save(spath,'ch_infor');
end

5.制作训练集和测试机

clear;
clc;
%% create train data and test data
for fi = 1:8
        path = ['d:/数据/球磨机/5/300/ch_' , num2str(fi),'.mat']
        spath1 = ['d:/数据/5/通道数据/ch' , num2str(fi),'/train_x.mat']
        spath2 = ['d:/数据/5/通道数据/ch' , num2str(fi),'/test_x.mat']
        load(path);
        index = 1;
        for i = 1:102
            for j=1:15
                train_x(index,:) = reshape( ch_infor(i,j,:),1,300);
                index=index+1;
            end
        end
        save(spath1, 'train_x');
        index = 1;
        for i = 1:102
            for j=16:22
                test_x(index,:) = reshape( ch_infor(i,j,:),1,300);
                index=index+1;
            end
        end
        save(spath2 , 'test_x');
        
end

最后做出的图为:

matlab处理采集的声音数据_第1张图片

这说明训练的模型还是有用的!可以把对应的模式给识别出来

你可能感兴趣的:(机器学习,java)