绘制录制声音时的频率很奇怪

时间:2013-05-01 00:34:56

标签: java android matlab audio signal-processing

我将生成的噪音存储在WAV文件中,我将在带有扬声器的Android智能手机上播放并录制声音并使用相同的智能手机将其另存为另一个WAV文件(仅2个独立的线程,1个用于演奏,1录音)。我可以成功获取录制的WAV文件。但是当我在Matlab中绘制两个WAV文件时,我在两个图之间有一个奇怪的区别,The button one is the recorded signal

如您所见,录制的信号频率远高于原始信号。我无法用自己的耳朵区分这2个WAV文件。有人能告诉我究竟是什么导致了这种差异?谢谢! My original wav file is hereMy recorded WAV file is here和我录制声音的代码是:

private int bufferSizeInBytes = 0;  
 private int audioSource = MediaRecorder.AudioSource.MIC;  
    private static int sampleRateInHz = 48000;   
    private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;  
    private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;  

      private void creatAudioRecord() {  

            bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,  
                    channelConfig, audioFormat);  

            audioRecord = new AudioRecord(audioSource, sampleRateInHz,  
                    channelConfig, audioFormat, bufferSizeInBytes);  
        }  

      class AudioRecordThread implements Runnable {  
            @Override  
            public void run() {  
                writeDateTOFile2();
                copyWaveFile(AudioName, NewAudioName);
            }  
        }  
private void writeDateTOFile2(byte[] audiodata) {  

            FileOutputStream fos = null;  
            int readsize = 0;  
            try {  
                File file = new File(AudioName2);  
                if (file.exists()) {  
                    file.delete();  
                }  
                fos = new FileOutputStream(file);
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
                    try {  
                        fos.write(audiodata);  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  


            try {  
                fos.close();
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  



        @SuppressLint("NewApi")
        private void copyWaveFile(String inFilename, String outFilename) {  
            FileInputStream in = null;  
            FileOutputStream out = null;  
            long totalAudioLen = 0;  
            long totalDataLen = totalAudioLen + 36;  
            long longSampleRate = sampleRateInHz;  
            int channels = 1;  
            long byteRate = 16 * sampleRateInHz * channels / 8;  
            byte[] data = new byte[bufferSizeInBytes];  

            try {  
                in = new FileInputStream(inFilename);  
                out = new FileOutputStream(outFilename);  
                totalAudioLen = in.getChannel().size();  
                totalDataLen = totalAudioLen + 36;  
                WriteWaveFileHeader(out, totalAudioLen, totalDataLen,  
                        longSampleRate, channels, byteRate);  
                while (in.read(data) != -1) {  
                    out.write(data);  
                }  
                in.close();  
                out.close();  
            } catch (FileNotFoundException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
          }

        }  


        private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen,  
                long totalDataLen, long longSampleRate, int channels, long byteRate)  
                throws IOException {  
            byte[] header = new byte[44];  
            header[0] = 'R'; // RIFF/WAVE header  
            header[1] = 'I';  
            header[2] = 'F';  
            header[3] = 'F';  
            header[4] = (byte) (totalDataLen & 0xff);  
            header[5] = (byte) ((totalDataLen >> 8) & 0xff);  
            header[6] = (byte) ((totalDataLen >> 16) & 0xff);  
            header[7] = (byte) ((totalDataLen >> 24) & 0xff);  
            header[8] = 'W';  
            header[9] = 'A';  
            header[10] = 'V';  
            header[11] = 'E';  
            header[12] = 'f'; // 'fmt ' chunk  
            header[13] = 'm';  
            header[14] = 't';  
            header[15] = ' ';  
            header[16] = 16; // 4 bytes: size of 'fmt ' chunk  
            header[17] = 0;  
            header[18] = 0;  
            header[19] = 0;  
            header[20] = 1; // format = 1  
            header[21] = 0;  
            header[22] = (byte) channels;  
            header[23] = 0;  
            header[24] = (byte) (longSampleRate & 0xff);  
            header[25] = (byte) ((longSampleRate >> 8) & 0xff);  
            header[26] = (byte) ((longSampleRate >> 16) & 0xff);  
            header[27] = (byte) ((longSampleRate >> 24) & 0xff);  
            header[28] = (byte) (byteRate & 0xff);  
            header[29] = (byte) ((byteRate >> 8) & 0xff);  
            header[30] = (byte) ((byteRate >> 16) & 0xff);  
            header[31] = (byte) ((byteRate >> 24) & 0xff);  
            header[32] = (byte) (2 * 16 / 8); // block align  
            header[33] = 0;  
            header[34] = 16; // bits per sample  
            header[35] = 0;  
            header[36] = 'd';  
            header[37] = 'a';  
            header[38] = 't';  
            header[39] = 'a';  
            header[40] = (byte) (totalAudioLen & 0xff);  
            header[41] = (byte) ((totalAudioLen >> 8) & 0xff);  
            header[42] = (byte) ((totalAudioLen >> 16) & 0xff);  
            header[43] = (byte) ((totalAudioLen >> 24) & 0xff);  
            out.write(header, 0, 44);  
        }  

我在Matlab中绘制这两个WAV文件的代码是:

[wave,fs]=wavread('jamming3.wav'); 
wave=wave(:,[1]);
t=0:1/fs:(length(wave)-1)/fs;
 magnwave=abs(fft(wave));
 phasewave=angle(fft(wave));
 sub1_L=magnwave.*exp(i*phasewave);
 sub1_L=ifft(sub1_L);
 q=(abs(sub1_L).*cos(angle(sub1_L)))*(2^15-1);
 q=q(1000:length(q),1);
% plot(q);
% axis([44000,45000,-2^15+1,+2^15-1]);



test = 950;

t=0:1/fs:(length(wave)-1)/fs;
z = t([1],:);
wave1=wave(:,[1])*(2^15-1);
figure; hold on;
grid on;
box on;
plot(1:length(q), q,'-g','Displayname','Original noise');
%scatter(1:length(wave1), wave1,5, [1 0 0],'Displayname','Noise sample');
%scatter(1:length(q),q(:,1)-wave1,5,'Displayname','Error');
%axis([test,test+200,-40000,40000]);
axis([0,300,-40000,40000]);
hold on;
legend show;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[wave,fs]=wavread('recorded noise2.wav'); 
wave=wave(:,[1]);
%wave=wave(1000:length(wave));
%sub1=sub1(1000:length(sub1));

t=0:1/fs:(length(wave)-1)/fs;
 magnwave=abs(fft(wave));
 phasewave=angle(fft(wave));
 sub1_L=magnwave.*exp(i*phasewave);
 sub1_L=ifft(sub1_L);
 q1=(abs(sub1_L).*cos(angle(sub1_L)))*(2^15-1);
 q1=q1(15252:length(q1),1);
% plot(q);
% axis([44000,45000,-2^15+1,+2^15-1]);


 offsets = 0;


t=0:1/fs:(length(wave)-1)/fs;
z = t([1],:);
wave1=wave(:,[1])*(2^15-1);
figure; hold on;
grid on;
box on;
plot(1:length(q1), q1*1.25,'-g','Displayname','Recovered signal');
%scatter(1:length(wave1), wave1,5, [1 0 0],'Displayname','Noise sample');
%scatter(1:length(q),q(:,1)-wave1,5,'Displayname','Error');
axis([offsets,offsets+300,-40000,40000]);
%axis([0,300,-40000,40000]);
hold on;
legend show;

更新#1:对不起,我正在绘制的部分是我放在WAV文件中用于同步的标题,因此我可以对其后的数据进行进一步处理。 my plotted wave using voice recognization channel

更新#2:我最近更新了我的代码,使用语音识别通道录制声音,它看起来更好,但频率仍然是问题。我新录制的噪音也会在上面的链接中更新。

更新#3:我只是通过使用size(x)在matlab中显示了这2个wav文件的确切大小,并且它显示从原始文件中提取的数组大约是100k,而记录的wav文件中的数据是只有50k,基于这两个文件大小相同的面孔,我认为这可能是问题所在。我的录音过程有什么问题吗?

更新#4:我刚刚意识到由wavplay()函数无法直接在matlab中播放由智能手机生成的录制的wav文件,原因是什么?我上面的wav写入函数可能存在一些问题,但它是生成WAV文件的标准方法:(

更新#5:知道所有设备都支持44.1 khz,所以我将原始信号更改为44.1 khz,仍然没有好处。 :(

1 个答案:

答案 0 :(得分:1)

看起来好像一个或多个文件指定的采样率与记录或正在播放数据的采样率不匹配。