annotate carfac/sai.cc @ 643:8b70f4cf00c7

Additional changes to C++ CARFAC on the basis of ronw's comments on r289. Moved CARFAC::Design to CARFAC::CARFAC and CARFAC::Reset(), moved carfac_common.h to common.h, CARFACDetect to carfac_util.h/cc, FloatArray and Float2dArray to ArrayX and ArrayXX, improved variable naming, made a start on improved commenting documentation.
author alexbrandmeyer
date Tue, 04 Jun 2013 18:30:22 +0000
parents 20f64146c2ce
children 749b5aed61f6
rev   line source
ronw@642 1 // Copyright 2013, Google, Inc.
ronw@642 2 // Author: Ron Weiss <ronw@google.com>
ronw@642 3 //
ronw@642 4 // This C++ file is part of an implementation of Lyon's cochlear model:
ronw@642 5 // "Cascade of Asymmetric Resonators with Fast-Acting Compression"
ronw@642 6 // to supplement Lyon's upcoming book "Human and Machine Hearing"
ronw@642 7 //
ronw@642 8 // Licensed under the Apache License, Version 2.0 (the "License");
ronw@642 9 // you may not use this file except in compliance with the License.
ronw@642 10 // You may obtain a copy of the License at
ronw@642 11 //
ronw@642 12 // http://www.apache.org/licenses/LICENSE-2.0
ronw@642 13 //
ronw@642 14 // Unless required by applicable law or agreed to in writing, software
ronw@642 15 // distributed under the License is distributed on an "AS IS" BASIS,
ronw@642 16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ronw@642 17 // See the License for the specific language governing permissions and
ronw@642 18 // limitations under the License.
ronw@642 19
ronw@642 20 #include <assert.h>
ronw@642 21
ronw@642 22 #include "sai.h"
ronw@642 23
ronw@642 24 SAI::SAI(const SAIParams& params) : params_(params) {
ronw@642 25 assert(params_.window_width > params_.width &&
ronw@642 26 "SAI window_width must be larger than width.");
ronw@642 27
ronw@642 28 int buffer_width = params_.width +
ronw@642 29 static_cast<int>((1 + static_cast<float>(params_.n_window_pos - 1)/2) *
ronw@642 30 params_.window_width);
ronw@642 31 input_buffer_.setZero(params_.n_ch, buffer_width);
ronw@642 32 output_buffer_.setZero(params_.n_ch, params_.width);
ronw@642 33
ronw@642 34 window_.setLinSpaced(params_.window_width, kPi / params_.window_width, kPi)
ronw@642 35 .sin();
ronw@642 36 }
ronw@642 37
alexbrandmeyer@643 38 void SAI::RunSegment(const std::vector<ArrayX>& input,
alexbrandmeyer@643 39 ArrayXX* output_frame) {
ronw@642 40 assert(!input.empty() || input.size() <= params_.window_width &&
ronw@642 41 "Unexpected input size.");
ronw@642 42 assert(input[0].size() == params_.n_ch &&
ronw@642 43 "Unexpected input frame size.");
ronw@642 44
ronw@642 45 // Append new data to the input buffer.
ronw@642 46 int n_shift = input.size();
ronw@642 47 int shift_width = input_buffer_.cols() - n_shift;
ronw@642 48 input_buffer_.topLeftCorner(params_.n_ch, shift_width).swap(
ronw@642 49 input_buffer_.block(0, n_shift, params_.n_ch, shift_width));
ronw@642 50 for (int i = 0; i < input.size(); ++i) {
ronw@642 51 input_buffer_.block(0, shift_width + i, input[i].size(), 1) = input[i];
ronw@642 52 }
ronw@642 53 // Zero-pad the buffer if necessary.
ronw@642 54 if (input.size() < params_.window_width) {
ronw@642 55 int pad_width = params_.window_width - input.size();
ronw@642 56 input_buffer_.topRightCorner(params_.n_ch, pad_width).setZero();
ronw@642 57 }
ronw@642 58
ronw@642 59 StabilizeSegment(input_buffer_, &output_buffer_);
ronw@642 60 *output_frame = output_buffer_;
ronw@642 61 }
ronw@642 62
alexbrandmeyer@643 63 void SAI::StabilizeSegment(const ArrayXX& input_buffer,
alexbrandmeyer@643 64 ArrayXX* output_buffer) const {
ronw@642 65 // Windows are always approximately 50% overlapped.
ronw@642 66 float window_hop = params_.window_width / 2;
ronw@642 67 int window_start = (input_buffer.cols() - params_.window_width) -
ronw@642 68 (params_.n_window_pos - 1) * window_hop;
ronw@642 69 int window_range_start = window_start - params_.future_lags - 1;
ronw@642 70 int offset_range_start = window_start - params_.width;
ronw@642 71 assert(offset_range_start >= 0);
ronw@642 72 for (int i = 0; i < params_.n_ch; ++i) {
ronw@642 73 // TODO(ronw): Rename this here and in the Matlab code since the
ronw@642 74 // input doesn't have to contain naps.
alexbrandmeyer@643 75 const ArrayX& nap_wave = input_buffer.row(i);
ronw@642 76 // TODO(ronw): Smooth row.
ronw@642 77
ronw@642 78 for (int w = 0; w < params_.n_window_pos; ++w) {
ronw@642 79 int current_window_offset = w * window_hop;
ronw@642 80 // Choose a trigger point.
ronw@642 81 int trigger_time;
alexbrandmeyer@643 82 const ArrayX& trigger_window =
ronw@642 83 nap_wave.segment(window_range_start + current_window_offset,
ronw@642 84 params_.window_width);
ronw@642 85 FPType peak_val = (trigger_window * window_).maxCoeff(&trigger_time);
ronw@642 86 if (peak_val <= 0) {
ronw@642 87 peak_val = window_.maxCoeff(&trigger_time);
ronw@642 88 }
ronw@642 89 trigger_time += current_window_offset;
ronw@642 90
ronw@642 91 // Blend the window following the trigger into the output
ronw@642 92 // buffer, weighted according to the the trigger strength (0.05
ronw@642 93 // to near 1.0).
ronw@642 94 FPType alpha = (0.025 + peak_val) / (0.5 + peak_val);
ronw@642 95 output_buffer->row(i) *= 1 - alpha;
ronw@642 96 output_buffer->row(i) += alpha *
ronw@642 97 nap_wave.segment(trigger_time + offset_range_start, params_.width);
ronw@642 98 }
ronw@642 99 }
ronw@642 100 }