annotate trunk/matlab/bmm/carfac/CARFAC_hacking.m @ 563:fb602edc2d55

Separate the OHC nonlinear function into new file CARFAC_OHC_NLF.m. Update Design doc a bit. Add optional outputs and hacks that I used with Roy to look at distortion effects in OHC.
author dicklyon@google.com
date Sat, 12 May 2012 04:31:59 +0000
parents 3dff17554c6d
children 3e2e0ab4f708
rev   line source
tom@516 1 % Copyright 2012, Google, Inc.
tom@516 2 % Author: Richard F. Lyon
tom@516 3 %
tom@516 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@516 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@516 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@516 7 %
tom@516 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@516 9 % you may not use this file except in compliance with the License.
tom@516 10 % You may obtain a copy of the License at
tom@516 11 %
tom@516 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@516 13 %
tom@516 14 % Unless required by applicable law or agreed to in writing, software
tom@516 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@516 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@516 17 % See the License for the specific language governing permissions and
tom@516 18 % limitations under the License.
tom@516 19
tom@516 20 %% Test/demo hacking for CARFAC Matlab stuff:
tom@516 21
tom@516 22 clear variables
tom@516 23
tom@516 24 %%
dicklyon@559 25 use_plan_file = 1;
dicklyon@536 26 if use_plan_file
dicklyon@536 27
dicklyon@536 28 file_signal = wavread('plan.wav');
dicklyon@563 29 % file_signal = file_signal(8100+(1:20000)); % trim for a faster test
dicklyon@563 30 file_signal = file_signal(10000+(1:10000)); % trim for a faster test
dicklyon@536 31
dicklyon@536 32 else
dicklyon@563 33 flist = [1000];
dicklyon@563 34 alist = [1];
dicklyon@563 35 flist = 1000;
dicklyon@563 36 alist = 1;
dicklyon@563 37 sine_signal = 0;
dicklyon@563 38 times = (0:19999)' / 22050;
dicklyon@563 39 for fno = 1:length(flist)
dicklyon@563 40 sine_signal = sine_signal + alist(fno)*sin(flist(fno)*2*pi*times);
dicklyon@563 41 end
dicklyon@563 42 growth_power = 0; % use 0 for flat, 4 or more for near exponential
dicklyon@563 43 file_signal = 1.0 * (sine_signal .* (times/max(times)).^growth_power);
dicklyon@536 44 end
tom@516 45
tom@516 46 % repeat with negated signal to compare responses:
dicklyon@517 47 % file_signal = [file_signal; -file_signal];
tom@516 48
tom@516 49 % make a long test signal by repeating at different levels:
dicklyon@563 50 do_distortion_figs = 0; % use 1 for distortion figure hack....
dicklyon@563 51 if do_distortion_figs
dicklyon@563 52 dB_list = -40:20:0
dicklyon@563 53 else
dicklyon@563 54 dB_list = -80:20:60
dicklyon@563 55 end
dicklyon@563 56 % dB = dB_list(1);
dicklyon@563 57 % test_signal = 10^(dB/20)* file_signal(1:4000) % lead-in [];
dicklyon@563 58 test_signal = [];
dicklyon@563 59 for dB = dB_list
tom@516 60 test_signal = [test_signal; file_signal * 10^(dB/20)];
tom@516 61 end
tom@516 62
tom@516 63
tom@516 64 %% Run mono, then stereo test:
tom@516 65
tom@516 66 agc_plot_fig_num = 6;
tom@516 67
dicklyon@563 68 for n_ears = 1 % 1:2
dicklyon@561 69
dicklyon@561 70 CF_struct = CARFAC_Design(n_ears); % default design
dicklyon@563 71
dicklyon@559 72 if n_ears == 2
dicklyon@559 73 % For the 2-channel pass, add a silent second channel:
dicklyon@559 74 test_signal = [test_signal, zeros(size(test_signal))];
dicklyon@559 75 end
dicklyon@559 76
dicklyon@561 77 CF_struct = CARFAC_Init(CF_struct);
dicklyon@563 78
dicklyon@563 79 [CF_struct, nap_decim, nap, BM, ohc, agc] = CARFAC_Run(CF_struct, test_signal, ...
dicklyon@534 80 agc_plot_fig_num);
dicklyon@563 81
dicklyon@563 82 % nap = deskew(nap); % deskew doesn't make much difference
dicklyon@563 83
dicklyon@563 84 % dB_BM = 10/log(10) * log(filter(1, [1, -0.995], BM(:, 20:50, :).^2));
dicklyon@563 85 dB_BM = 10/log(10) * log(filter(1, [1, -0.995], BM(:, :, :).^2));
dicklyon@563 86
dicklyon@559 87 % only ear 1:
dicklyon@559 88 MultiScaleSmooth(dB_BM(5000:200:end, :, 1), 1);
dicklyon@563 89
dicklyon@534 90 % Display results for 1 or 2 ears:
dicklyon@534 91 for ear = 1:n_ears
dicklyon@534 92 smooth_nap = nap_decim(:, :, ear);
dicklyon@534 93 if n_ears == 1
tom@516 94 mono_max = max(smooth_nap(:));
tom@516 95 end
dicklyon@534 96 figure(3 + ear + n_ears) % Makes figures 5, ...
tom@516 97 image(63 * ((max(0, smooth_nap)/mono_max)' .^ 0.5))
tom@516 98 title('smooth nap from nap decim')
tom@516 99 colormap(1 - gray);
tom@516 100 end
dicklyon@563 101
tom@516 102 % Show resulting data, even though M-Lint complains:
tom@516 103 CF_struct
dicklyon@563 104 CF_struct.ears(1).CAR_state
dicklyon@563 105 CF_struct.ears(1).AGC_state
tom@516 106 min_max_decim = [min(nap_decim(:)), max(nap_decim(:))]
dicklyon@563 107
dicklyon@563 108 %%
dicklyon@563 109 if do_distortion_figs
dicklyon@563 110 channels = [38, 39, 40];
dicklyon@563 111 times = 0000 + (3200:3599);
dicklyon@563 112 smoothed_ohc = ohc;
dicklyon@563 113 epsi = 0.4;
dicklyon@563 114 % smoothed_ohc = filter(epsi, [1, -(1-epsi)], smoothed_ohc);
dicklyon@563 115 % smoothed_ohc = filter(epsi, [1, -(1-epsi)], smoothed_ohc);
dicklyon@563 116
dicklyon@563 117 figure(101); hold off
dicklyon@563 118 plot(smoothed_ohc(times, channels))
dicklyon@563 119 hold on
dicklyon@563 120 plot(smoothed_ohc(times, channels(2)), 'k-', 'LineWidth', 1.5)
dicklyon@563 121 title('OHC')
dicklyon@563 122
dicklyon@563 123 figure(105); hold off
dicklyon@563 124 plot(agc(times, channels))
dicklyon@563 125 hold on
dicklyon@563 126 plot(agc(times, channels(2)), 'k-', 'LineWidth', 1.5)
dicklyon@563 127 title('AGC')
dicklyon@563 128
dicklyon@563 129 figure(103); hold off
dicklyon@563 130 plot(BM(times, channels))
dicklyon@563 131 hold on
dicklyon@563 132 plot(BM(times, channels(2)), 'k-', 'LineWidth', 1.5)
dicklyon@563 133 title('BM')
dicklyon@563 134
dicklyon@563 135 extra_damping = smoothed_ohc + agc;
dicklyon@563 136 figure(102); hold off
dicklyon@563 137 plot(extra_damping(times, channels))
dicklyon@563 138 hold on
dicklyon@563 139 plot(extra_damping(times, channels(2)), 'k-', 'LineWidth', 1.5)
dicklyon@563 140 title('extra damping')
dicklyon@563 141
dicklyon@563 142 distortion = -(extra_damping - smooth1d(extra_damping, 10)) .* BM;
dicklyon@563 143 distortion = filter(epsi, [1, -(1-epsi)], distortion);
dicklyon@563 144 distortion = filter(epsi, [1, -(1-epsi)], distortion);
dicklyon@563 145 % distortion = filter(epsi, [1, -(1-epsi)], distortion);
dicklyon@563 146 figure(104); hold off
dicklyon@563 147 plot(distortion(times, channels))
dicklyon@563 148 hold on
dicklyon@563 149 plot(distortion(times, channels(2)), 'k-', 'LineWidth', 1.5)
dicklyon@563 150 title('distortion')
dicklyon@563 151
dicklyon@563 152 end
dicklyon@563 153 %%
tom@516 154 end
tom@516 155
tom@516 156 % Expected result: Figure 3 looks like figure 2, a tiny bit darker.
tom@516 157 % and figure 4 is empty (all zero)