将'获取脉冲响应代码'从python转换为matlab

时间:2017-08-19 08:50:14

标签: python matlab

我正在尝试将python代码转换为Matlab。代码功能是从原始信号和记录信号中获得脉冲响应。 python代码从Joseph获得。我相信重写的代码与python代码相同。然而,从python和Matlab获得的脉冲响应是不同的。我在这做错了什么?希望听到一些建议和批评。

Comparison of impulse response from python(left) and matlab(right)

来自python(左)和matlab(右)的脉冲响应的比较

impulseresponse.m [Matlab]

<?php

$name_arr = array('raj','raj','ganesh','rahul','ganesh','mayur','raj','rahul');

$new_arr = array_count_values($name_arr);

arsort($new_arr);

$value=array();

foreach($new_arr as $key=>$val){

   $value= array_merge($value,array_fill(0,$val,$key));
}

print_r($value);

?>

pad.m [应该与python代码中的padarray功能相同]

[a,fs] = audioread('sweep.wav'); % sweep
[b,fs] = audioread('rec.wav'); % rec

a = pad(a,fs*50,fs*10);
b = pad(b,fs*50,fs*10);
[m,n] = size(b);
h = zeros(m,n); 

for chan = 1:2
    b1 = b(:,chan);
    b1 = filter20_20k(b1,fs);
    ffta = fft(a);
    fftb = fft(b1);
    ffth = fftb ./ ffta;
    h1 = ifft(ffth);
    h1 = filter20_20k(h1,fs);
    h(:,chan) = h1;
end

h = h(1:10*fs,:);
dB = 40;
h = h*power(10,dB*1.0/20);
h(:,1) = h(:,1) ./ max(abs(h(:,1))); % normalizing impulse response
h(:,2) = h(:,2) ./ max(abs(h(:,2)));
figure;
plot(h)

filter20_20k.m [功能与filter20_20k(x,sr)相同]

function y = pad(data, t_full, t_pre)
[row_dim,col_dim] = size(data);
t_post = t_full - row_dim - t_pre;
if t_post > 0
    if col_dim == 1
        y = [zeros(t_pre,1);data;zeros(t_post,1)];
    else
        y1 = [zeros(t_pre,1);data(:,1);zeros(t_post,1)];
        y2 = [zeros(t_pre,1);data(:,2);zeros(t_post,1)];
        y = [y1,y2];
    end 
else
    if col_dim == 1
        y = [zeros(t_pre,1);data(1:t_full - t_pre,1)];
    else
        y1 = [zeros(t_pre,1);data(1:t_full - t_pre,1)];
        y2 = [zeros(t_pre,1);data(1:t_full - t_pre,2)];
        y = [y1,y2];
    end
end
end
约瑟夫·欧内斯特的

python代码

function y = filter20_20k(x,fs)
nyq = 0.5*fs;
[z,p,k] = butter(5,[20.0/nyq,20000.0/nyq],'bandpass');
sos = zp2sos(z,p,k);
y = sosfilt(sos,x);
end

1 个答案:

答案 0 :(得分:0)

现在管理将Joseph's impulseresponse.py python codes转换为matlab。将此放在这里以供人们使用它的未来可能性。

<强> impulseresponse.m

[a,fs] = audioread('sweep.wav'); % sweep
[b,fs] = audioread('rec.wav'); % rec

a = pad(a,fs*50,fs*10);
b = pad(b,fs*50,fs*10);
[m,n] = size(b);
h = zeros(m,n); 

for chan = 1:2
    b1 = b(:,chan);
    b1 = filter20_20k(b1,fs);
    ffta = rfft(a);
    fftb = rfft(b1);
    ffth = fftb ./ ffta;
    h1 = irfft(ffth);
    h1 = filter20_20k(h1,fs);
    h(:,chan) = h1;
end

h = h(1:10*fs,:);
dB = 40;
h = h*power(10,dB*1.0/20);

audiowrite('heck.wav',h,fs,'BitsPerSample',24);
[y,fs] = audioread('heck.wav');
figure;
plot(y)

<强> pad.m

function y = pad(data, t_full, t_pre)
[row_dim,col_dim] = size(data);
t_post = t_full - row_dim - t_pre;
if t_post > 0
    if col_dim == 1
        y = [zeros(t_pre,1);data;zeros(t_post,1)];
    else
        y1 = [zeros(t_pre,1);data(:,1);zeros(t_post,1)];
        y2 = [zeros(t_pre,1);data(:,2);zeros(t_post,1)];
        y = [y1,y2];
    end 
else
    if col_dim == 1
        y = [zeros(t_pre,1);data(1:t_full - t_pre,1)];
    else
        y1 = [zeros(t_pre,1);data(1:t_full - t_pre,1)];
        y2 = [zeros(t_pre,1);data(1:t_full - t_pre,1)];
        y = [y1,y2];
    end
end  
end

<强> filter20_20k.m

function y = filter20_20k(x,fs)
nyq = 0.5*fs;
[z,p,k] = butter(5,[20.0/nyq,20000.0/nyq],'bandpass');
sos = zp2sos(z,p,k);
y = sosfilt(sos,x);
end

rfft.m和irrft.m

转到此link。归功于Dmitri Chubarov。