Frequency Response to IR calculation yields shallower Magnitude level?
5 views (last 30 days)
Show older comments
Michaela Warnecke
on 4 Oct 2023
Commented: Mathieu NOE
on 9 Oct 2023
Hi all,
I have a frequency response curve of an audio recording. All I know is that is spans some range (see below) and was (most likely) sampled at fs = 48k.
I need to convert it to an IR to apply some specific filtering and am using the following code for that conversion:
FFT_length = 1024;
fs = 48e3;
%freq_axis = 1xN vector containing the range of frequencies in Hz ([71:20e3] in some octave steps)
%FR_data = 1xN vectora containing the magnitde values at each freq in freq_axis
%convert frequencies to normalized values to generate IR filters
filter_freqs = [0 freq_axis./(fs/2) 1];
%Create time-domain IRs
IR_sig = fir2(FFT_length, filter_freqs, [FR_data(1)-1 FR_data FR_data(end)+1]);
This creates an IR signal, which I then convert back into the frequency domain to validate against the original FR using the following:
cutoff_freq = fs/2;
HRTF_mag = abs(fft(IR_sig, FFT_length));
%Return only from 0 to pi
Mag = 20*log10(HRTF_mag(1:(FFT_length/2)+1));
f_vec = [0:(cutoff_freq*2)/FFT_length:cutoff_freq];
When I plot the original Frequency response curve (FR_data, left) and also plot the IR-converted, frequency domain signal (Mag, right), I can see a close match in the frequency domain, but the magnitude is entirely off. You can see that the original data (left) shows a difference of ~30 dB between the notch at 2.5k and the peak at 4.1k. The same notch and peak are only about 5dB apart in the deduced FR (Mag).
What am I missing/doing wrong?
2 Comments
Paul
on 6 Oct 2023
Hi Michaela,
You're more likely to get help if you upload the data needed to run your code and recreate your results. Save freq_axis and FR_data (if that's all that's needed) to a .mat file and add the file to your question using the paperclip icon in the Insert ribbon. Also, can you add the plotting commands used to make the plots.
Accepted Answer
More Answers (1)
Mathieu NOE
on 6 Oct 2023
hi
I have done something similar in the past - see below FYI
I start with a given FIR filter then computation of the transfer function and backward to the FIR ( = impulse response of a TF)
%
Fs = 1e3;
coef_fs = 2; % 2 for Nyquist (exact match) , measurement factor can be 2.56
Freq = linspace(0,Fs/coef_fs,200); % make sure freq vector goes up to Fs/2
b = fir1(48,[0.2 0.3]); % Window-based FIR filter design
frf = freqz(b,1,Freq,Fs);
%% FIR obtained with ifft method
if mod(length(frf),2)==0 % iseven
frf_sym = conj(frf(end:-1:2));
else
frf_sym = conj(frf(end-1:-1:2));
end
fir = real(ifft([frf frf_sym]));
% resample fir (impulse response) if frf data is not provided up to Nyquits frequency (Fs/2)
corfactor = 2/coef_fs;
x_og = (1:length(fir));
x_cor = linspace(1,length(fir)*corfactor,length(fir));
fir = interp1(x_og,fir,x_cor);
fir = fir(1:50); % truncation is possible if fir decays enough
frfid = freqz(fir,1,Freq,Fs);
% alternative with invfreqz
ITER = 1e3;
% FIR filter design
NB = 50; %
NA = 0;
W = 2*pi*Freq/Fs;
Wt = ones(size(W));
TOL = 1e-2;
[fir2,A] = invfreqz(frf,W,NB,NA,Wt,ITER,TOL);
frfid2 = freqz(fir2,1,Freq,Fs);
figure(1)
subplot(311),plot(1:length(b),b,'-*',1:length(fir),fir,1:length(fir2),fir2);
legend('FIR model','identified FIR (fft)','identified FIR (invfreqz)');
xlabel('samples');
ylabel('amplitude');
subplot(312),plot(Freq,20*log10(abs(frf)),Freq,20*log10(abs(frfid)),Freq,20*log10(abs(frfid2)),'*');
legend('input model','identified FIR model (fft)','identified FIR model (invfreqz)');
xlabel('frequency (Hz)');
ylabel('FRF modulus (dB)');
subplot(313),plot(Freq,180/pi*angle(frf),Freq,180/pi*angle(frfid),Freq,180/pi*angle(frfid),'*');
legend('input model','identified FIR model (fft)','identified FIR model (invfreqz)');
xlabel('frequency (Hz)');
ylabel('FRF angle (°)');
2 Comments
See Also
Categories
Find more on Filter Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!