-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompFilter.m
64 lines (53 loc) · 1.86 KB
/
compFilter.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
64
function [ yr ] = compFilter( y, fs )
% Compensated filter used to filter single-channel signals.
% input : y - a single channel signal.
% fs - the sampling rate.
% output : yr - the modulated signal after filtering.
% Shu Wang 2019-09-20
%% TEST
% clear;
% close all;
% [y, fs] = audioread('./samples/genuine/0001.wav');
% y = y(:, 1);
% addpath('signalproc');
% addpath('spectrum');
%% FFT
[~, amp, ph] = fastFT( y(:,1), fs );
N = 2^nextpow2(length(y));
% generate inverse filter
load model/discFreqResp.mat;
[fList, fResp] = contFreqAnalysis( freqList, freqResp, N, fs );
[freqFilter, respFilter] = contInvFilter( fList, fResp, 0.001 ); % need to confirm
invFilter = zeros(size(amp));
invLen = length(freqFilter);
invFilter(1:invLen) = respFilter;
invFilter(end-invLen+1:end) = respFilter(end:-1:1);
% modulate the amplitude spectrum
amp_r = amp .* invFilter;
% signal power balance
pw = sum(amp(1:invLen) .^ 2);
pw_r = sum(amp_r(1:invLen) .^ 2);
scale = pw / pw_r; % power
amp_r = amp_r * sqrt(scale);
%% visualize
% spkFilter = zeros(size(amp));
% spkFilter(1:invLen) = fResp;
% spkFilter(end-invLen+1:end) = fResp(end:-1:1);
% amp_rr = amp_r .* spkFilter / sqrt(scale);
% figure();
% subplot(3,1,1); plot((1:invLen), amp(1:invLen)); title('genuine');
% subplot(3,1,2); plot((1:invLen), amp_r(1:invLen)); title('modulated');
% subplot(3,1,3); plot((1:invLen), amp_rr(1:invLen)); title('modulated replay');
%% IFFT
z = amp_r .* exp(1i * ph);
yr = real(ifft(z) * N / 2);
yr = yr(1:length(y));
%% visualize
% zr = amp_rr .* exp(1i * ph);
% yrr = real(ifft(zr) * N / 2);
% yrr = yrr(1:length(y));
% figure();
% subplot(3,1,1); plot(y(1000:2000, 1)); title('Genuine Audio');
% subplot(3,1,2); plot(yr(1000:2000, 1)); title('Modulated Audio');
% subplot(3,1,3); plot(yrr(1000:2000, 1)); title('ModReplay Audio (Sim)');
end