Selaa lähdekoodia

calibration: add sphear_af_calibrate, read reference signal, convolve with DRC generated equalization filter and filter and write to disk all recorded A format signals

Fernando Pablo Lopez-Lezcano 9 vuotta sitten
vanhempi
sitoutus
7290fafa25
1 muutettua tiedostoa jossa 70 lisäystä ja 0 poistoa
  1. 70 0
      calibration/src/sphear_af_calibrate.m

+ 70 - 0
calibration/src/sphear_af_calibrate.m

@@ -0,0 +1,70 @@
+%% -*- mode: octave -*-
+%%
+%% *SpHEAR Project
+%%
+%% sphear_af_calibrate: read reference microphone signal, filter with convolution
+%%                      kernel created by DRC and filter all recorded signals,
+%%                      calibrated A format files are written to disk
+%%
+%% usage:
+%%   [] = sphear_af_calibrate(measurements, keep, fade)
+%%
+%% keep: time to keep after main impulse in seconds
+%% fade: fade out time after keep in seconds
+%%
+%% Copyright 2016-2017, Fernando Lopez-Lezcano, All Rights Reserved
+%% nando@ccrma.stanford.edu
+%%
+%%  This program is free software: you can redistribute it and/or modify
+%%  it under the terms of the GNU General Public License as published by
+%%  the Free Software Foundation, either version 3 of the License, or
+%%  (at your option) any later version.
+%%
+%%  This program is distributed in the hope that it will be useful,
+%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
+%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+%%  GNU General Public License for more details.
+%%
+%%  You should have received a copy of the GNU General Public License
+%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+function [trunc] = sphear_af_calibrate(measurements, keep, fade);
+  %% the measurement from the reference microphone is processed through
+  %% DRC (Digital Room Correction) to create a convolution kernel that is
+  %% used to equalize the measurements
+  %%
+  %% read the convolution kernel
+  [ref, Fs] = wavread('drc/filter.wav');
+
+  %% window the DRC generated filter, eliminate the stuff before the main
+  %% peak and limit the length to the approximate useful length of the
+  %% original recorded impulse response
+  
+  [peaks, locs] = findpeaks(abs(ref));
+  [value, index] = max(peaks);
+  loc = locs(index);
+  
+  %% truncate before the main peak
+  b_len = 4;
+  before = tukeywin(2 * b_len, 0.9)(1:end/2) .* ref(loc-2*b_len:loc-b_len-1);
+  %% useful samples
+  samples = keep * Fs/2;
+  during = ref(loc-b_len:loc+samples-1);
+  %% fade out after useful samples
+  a_len = fade * Fs/2;
+  after = tukeywin(2 * a_len, 0.9)(end/2:end) .* ref(loc+samples:loc+samples+a_len);
+  %% and the whole thing is:
+  trunc = cat(1, before, during, after);
+
+  capnames = cellstr(['LF'; 'RF'; 'LB'; 'RB']);
+  %% all requested measurements
+  for m = measurements
+    %% read in capsule signal and normalize it
+    for c = 1:4
+      sig = wavread(["../data/current/af/a-format-" sprintf("%03d", m) "-" char(capnames(c)) ".wav"]);
+      cal_sig(:,c) = conv(trunc, sig);
+      wavwrite(cal_sig(:,c), 32, ["../data/current/af/a-format-cal-" sprintf("%03d", m) "-" char(capnames(c)) ".wav"]);
+    end
+    wavwrite(cal_sig, 32, ["../data/current/af/a-format-cal-" sprintf("%03d", m) ".wav"]);
+  end
+end