0

我有一个生成的噪音存储在 WAV 文件中,我将在带有扬声器的 Android 智能手机上播放它并录制声音并使用同一智能手机将其保存为另一个 WAV 文件(只有 2 个单独的线程,1 个用于播放,1 个用于录音)。我可以成功获取录制的WAV文件。但是当我在 Matlab 中绘制两个 WAV 文件时,两个图之间有一个奇怪的区别,按钮一是记录的信号

如您所见,录制信号的频率比原始信号高得多。我无法用自己的耳朵区分这两个 WAV 文件。有人能告诉我究竟是什么导致了这种差异吗?谢谢! 我的原始wav文件在这里我录制的WAV文件在这里,我录制声音的代码是:

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 文件中用于同步的标题,因此我可以对其后面的数据进行进一步处理。 我使用语音识别通道绘制的波形

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

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

更新#4:我刚刚发现智能手机生成的录制的wav文件不能通过wavplay()函数直接在matlab中播放,是这个原因吗?我上面的wav编写函数可能有问题,但这是生成WAV文件的标准方法:(

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

4

1 回答 1

1

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

于 2013-05-01T06:20:55.680 回答