changeset 692:2d432ff51f64

Update interface to have separate Reset() and Redesign(params) methods.
author ronw@google.com
date Wed, 12 Jun 2013 19:24:29 +0000
parents d04114ac773d
children 3d749a008b87
files trunk/carfac/carfac.cc trunk/carfac/carfac.h trunk/carfac/ear.cc trunk/carfac/ear.h trunk/carfac/sai.cc trunk/carfac/sai.h
diffstat 6 files changed, 61 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/carfac/carfac.cc	Tue Jun 11 22:05:10 2013 +0000
+++ b/trunk/carfac/carfac.cc	Wed Jun 12 19:24:29 2013 +0000
@@ -33,7 +33,7 @@
 CARFAC::CARFAC(const int num_ears, const FPType sample_rate,
                const CARParams& car_params, const IHCParams& ihc_params,
                const AGCParams& agc_params) {
-  Reset(num_ears, sample_rate, car_params, ihc_params, agc_params);
+  Redesign(num_ears, sample_rate, car_params, ihc_params, agc_params);
 }
 
 CARFAC::~CARFAC() {
@@ -42,9 +42,9 @@
   }
 }
 
-void CARFAC::Reset(const int num_ears, const FPType sample_rate,
-                   const CARParams& car_params, const IHCParams& ihc_params,
-                   const AGCParams& agc_params) {
+void CARFAC::Redesign(const int num_ears, const FPType sample_rate,
+                      const CARParams& car_params, const IHCParams& ihc_params,
+                      const AGCParams& agc_params) {
   num_ears_ = num_ears;
   sample_rate_ = sample_rate;
   car_params_ = car_params;
@@ -55,14 +55,14 @@
   while (pole_hz > car_params_.min_pole_hz) {
     ++num_channels_;
     pole_hz = pole_hz - car_params_.erb_per_step *
-    ERBHz(pole_hz, car_params_.erb_break_freq, car_params_.erb_q);
+        ERBHz(pole_hz, car_params_.erb_break_freq, car_params_.erb_q);
   }
   pole_freqs_.resize(num_channels_);
   pole_hz = car_params_.first_pole_theta * sample_rate_ / (2 * kPi);
   for (int channel = 0; channel < num_channels_; ++channel) {
     pole_freqs_(channel) = pole_hz;
     pole_hz = pole_hz - car_params_.erb_per_step *
-    ERBHz(pole_hz, car_params_.erb_break_freq, car_params_.erb_q);
+        ERBHz(pole_hz, car_params_.erb_break_freq, car_params_.erb_q);
   }
   max_channels_per_octave_ = log(2) / log(pole_freqs_(0) / pole_freqs_(1));
   CARCoeffs car_coeffs;
@@ -75,8 +75,8 @@
   ears_.reserve(num_ears_);
   for (int i = 0; i < num_ears_; ++i) {
     if (ears_.size() > i && ears_[i] != NULL) {
-      // Reset any existing ears.
-      ears_[i]->Reset(num_channels_, car_coeffs, ihc_coeffs, agc_coeffs);
+      // Reinitialize any existing ears.
+      ears_[i]->Redesign(num_channels_, car_coeffs, ihc_coeffs, agc_coeffs);
     } else {
       ears_.push_back(
           new Ear(num_channels_, car_coeffs, ihc_coeffs, agc_coeffs));
@@ -84,6 +84,12 @@
   }
 }
 
+void CARFAC::Reset() {
+  for (Ear* ear : ears_) {
+    ear->Reset();
+  }
+}
+
 void CARFAC::RunSegment(const vector<vector<float>>& sound_data,
                         const int32_t start, const int32_t length,
                         const bool open_loop, CARFACOutput* seg_output) {
--- a/trunk/carfac/carfac.h	Tue Jun 11 22:05:10 2013 +0000
+++ b/trunk/carfac/carfac.h	Wed Jun 12 19:24:29 2013 +0000
@@ -49,9 +49,14 @@
          const AGCParams& agc_params);
   ~CARFAC();
 
-  void Reset(const int num_ears, const FPType sample_rate,
-             const CARParams& car_params, const IHCParams& ihc_params,
-             const AGCParams& agc_params);
+  // Reinitialize using the specified parameters.
+  void Redesign(const int num_ears, const FPType sample_rate,
+                const CARParams& car_params, const IHCParams& ihc_params,
+                const AGCParams& agc_params);
+
+  // Reset the internal state so that subsequent calls to RunSegment are
+  // independent of previous calls.  Does not modify the filterbank design.
+  void Reset();
 
   // Processes an individual sound segment and copies the model output to
   // seg_output.
--- a/trunk/carfac/ear.cc	Tue Jun 11 22:05:10 2013 +0000
+++ b/trunk/carfac/ear.cc	Wed Jun 12 19:24:29 2013 +0000
@@ -29,22 +29,26 @@
 Ear::Ear(const int num_channels, const CARCoeffs& car_coeffs,
          const IHCCoeffs& ihc_coeffs,
          const std::vector<AGCCoeffs>& agc_coeffs) {
-  Reset(num_channels, car_coeffs, ihc_coeffs, agc_coeffs);
+  Redesign(num_channels, car_coeffs, ihc_coeffs, agc_coeffs);
 }
 
-void Ear::Reset(const int num_channels, const CARCoeffs& car_coeffs,
-                const IHCCoeffs& ihc_coeffs,
-                const std::vector<AGCCoeffs>& agc_coeffs) {
+void Ear::Redesign(const int num_channels, const CARCoeffs& car_coeffs,
+                   const IHCCoeffs& ihc_coeffs,
+                   const std::vector<AGCCoeffs>& agc_coeffs) {
   num_channels_ = num_channels;
   car_coeffs_ = car_coeffs;
   ihc_coeffs_ = ihc_coeffs;
   agc_coeffs_ = agc_coeffs;
-  ResetCARState();
-  ResetIHCState();
-  ResetAGCState();
+  Reset();
 }
 
-void Ear::ResetCARState() {
+void Ear::Reset() {
+  InitCARState();
+  InitIHCState();
+  InitAGCState();
+}
+
+void Ear::InitCARState() {
   car_state_.z1_memory.setZero(num_channels_);
   car_state_.z2_memory.setZero(num_channels_);
   car_state_.za_memory.setZero(num_channels_);
@@ -55,7 +59,7 @@
   car_state_.dg_memory.setZero(num_channels_);
 }
 
-void Ear::ResetIHCState() {
+void Ear::InitIHCState() {
   ihc_state_.ihc_accum = ArrayX::Zero(num_channels_);
   if (!ihc_coeffs_.just_half_wave_rectify) {
     ihc_state_.ac_coupler.setZero(num_channels_);
@@ -70,7 +74,7 @@
   }
 }
 
-void Ear::ResetAGCState() {
+void Ear::InitAGCState() {
   int n_agc_stages = agc_coeffs_.size();
   agc_state_.resize(n_agc_stages);
   for (AGCState& stage_state : agc_state_) {
--- a/trunk/carfac/ear.h	Tue Jun 11 22:05:10 2013 +0000
+++ b/trunk/carfac/ear.h	Wed Jun 12 19:24:29 2013 +0000
@@ -39,9 +39,13 @@
       const IHCCoeffs& ihc_coeffs,
       const std::vector<AGCCoeffs>& agc_coeffs);
 
-  void Reset(const int num_channels, const CARCoeffs& car_coeffs,
-             const IHCCoeffs& ihc_coeffs,
-             const std::vector<AGCCoeffs>& agc_coeffs);
+  // Reinitialize using the specified parameters.
+  void Redesign(const int num_channels, const CARCoeffs& car_coeffs,
+                const IHCCoeffs& ihc_coeffs,
+                const std::vector<AGCCoeffs>& agc_coeffs);
+
+  // Reset the internal state.
+  void Reset();
 
   // These three methods apply the different steps of the model in sequence
   // to individual audio samples during the call to CARFAC::RunSegment.
@@ -102,9 +106,9 @@
 
  private:
   // Initializes the model state variables prior to runtime.
-  void ResetIHCState();
-  void ResetAGCState();
-  void ResetCARState();
+  void InitIHCState();
+  void InitAGCState();
+  void InitCARState();
 
   // Helper sub-functions called during the model runtime.
   void OHCNonlinearFunction(const ArrayX& velocities,
--- a/trunk/carfac/sai.cc	Tue Jun 11 22:05:10 2013 +0000
+++ b/trunk/carfac/sai.cc	Wed Jun 12 19:24:29 2013 +0000
@@ -22,6 +22,10 @@
 #include <assert.h>
 
 SAI::SAI(const SAIParams& params) : params_(params) {
+  Redesign(params);
+}
+
+void SAI::Redesign(const SAIParams& params) {
   assert(params_.window_width > params_.width &&
          "SAI window_width must be larger than width.");
 
@@ -35,6 +39,11 @@
       .sin();
 }
 
+void SAI::Reset() {
+  input_buffer_.setZero();
+  output_buffer_.setZero();
+}
+
 void SAI::RunSegment(const std::vector<ArrayX>& input,
                      ArrayXX* output_frame) {
   assert(!input.empty() || input.size() <= params_.window_width &&
--- a/trunk/carfac/sai.h	Tue Jun 11 22:05:10 2013 +0000
+++ b/trunk/carfac/sai.h	Wed Jun 12 19:24:29 2013 +0000
@@ -51,6 +51,12 @@
  public:
   explicit SAI(const SAIParams& params);
 
+  // Reinitialize using the specified parameters.
+  void Redesign(const SAIParams& params);
+
+  // Reset the internal state.
+  void Reset();
+
   // Fills output_frame with a params_.n_ch by params_.width SAI frame
   // computed from the given input frames.
   //