-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathIRcompactingKirkebyFilter.m
63 lines (56 loc) · 1.61 KB
/
IRcompactingKirkebyFilter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [ filt ] = IRcompactingKirkebyFilter( ir, ir_len, f_band, fs, reg )
% Compacting Kirkeby Filter
% Regularisation parameter
ereg = epsreg(ir_len*fs,f_band,fs,reg);
% Time-packing filtering
H = fft(ir, ir_len*fs);
C = conj(H) ./ (conj(H).*H + ereg);
filt = ifft(C);
end
function ereg = epsreg(Nfft, f_band, fs, reg)
f1=f_band(1);
f2=f_band(2);
reg_in=reg(1);
reg_out=reg(2);
if f1 > 0 && f2 < fs/2
freq=(0:fs/(Nfft-1):fs/2)';
f1e=f1-f1/3;
f2e=f2+f2/3;
if f1e < freq(1)
f1e=f1;
f1=f1+1;
end
if f2e > freq(end)
f2e=f2+1;
end
% regularization B with 1/3 octave interpolated transient edges
B=interp1([0 f1e f1 f2 f2e freq(end)],[reg_out reg_out reg_in reg_in reg_out reg_out],freq,'PCHIP');
B = db2mag(-B); % from dB to linear
B=vertcat(B,B(end:-1:1));
b=ifft(B,'symmetric');
b=circshift(b,Nfft/2);
b=0.5*(1-cos(2*pi*(1:Nfft)'/(Nfft+1))).*b;
b=minph(b); % make regularization minimum phase
B=fft(b,Nfft);
else
B=0;
end
ereg = (conj(B).*B);
end
% calculate minimum phase component of impulse response
function [h_min] = minph(h)
n = length(h);
h_cep = real(ifft(log(abs(fft(h(:,1))))));
odd = fix(rem(n,2));
wn = [1; 2*ones((n+odd)/2-1,1) ; ones(1-rem(n,2),1); zeros((n+odd)/2-1,1)];
h_min = zeros(size(h(:,1)));
h_min(:) = real(ifft(exp(fft(wn.*h_cep(:)))));
if size(h,2)==2
h_cep = real(ifft(log(abs(fft(h(:,2))))));
odd = fix(rem(n,2));
wn = [1; 2*ones((n+odd)/2-1,1) ; ones(1-rem(n,2),1); zeros((n+odd)/2-1,1)];
h_minr = zeros(size(h(:,2)));
h_minr(:) = real(ifft(exp(fft(wn.*h_cep(:)))));
h_min=[h_min h_minr];
end
end