tomwalters@0: function [pdampsmod, agcstate] = AGCdampStep(detect, pdamps, ... tomwalters@0: agcepsilons, agcgains, agcstate, agcfactor); tomwalters@0: % tomwalters@0: tomwalters@0: % function [pdampsmod, agcstate] = AGCdampStep(detect, pdamps, tomwalters@0: % agcepsilons, agcgains, agcstate); tomwalters@0: % tomwalters@0: % update the dampings and agcstates based on present outputs tomwalters@0: % including 50% stereo coupling if two channels based on size of tomwalters@0: % agcstate is twice the number of channels tomwalters@0: tomwalters@0: if nargin < 6 tomwalters@0: agcfactor=12; tomwalters@0: end tomwalters@0: tomwalters@0: Nch = length(pdamps); tomwalters@0: Ntracks = size(detect,2); % two columns for stereo tomwalters@0: Nstages = length(agcepsilons); tomwalters@0: tomwalters@0: tomwalters@0: if length(detect)==0, tomwalters@0: Ntracks = 1; % mono default tomwalters@0: if size(agcstate,2)>Nstages tomwalters@0: Ntracks = round(size(agcstate,2)/Nstages); % pass in agcstate big tomwalters@0: % enough to do stereo tomwalters@0: end tomwalters@0: detect = DetectFun(0.0)*ones(Nch,1); tomwalters@0: %agcstate = 1.2*detect*ones(1,Ntracks*Nstages); % a detect-dependent tomwalters@0: %hack to initialize damping tomwalters@0: rep = 1+rem((1:Ntracks*Nstages)-1,Nstages); tomwalters@0: agcstate = 1.2*detect*agcgains(rep); % rep is like [1 2 3 4 1 2 3 4] tomwalters@0: detect = DetectFun(0.0)*ones(Nch,Ntracks); tomwalters@0: end tomwalters@0: tomwalters@0: agcepsleft = 0.3; % 0.15; tomwalters@0: agcepsright = 0.3; % 0.15; tomwalters@0: spacecoeffs = [agcepsleft, 1.0-agcepsleft-agcepsright, agcepsright]; tomwalters@0: tomwalters@0: for k = 1:Ntracks % track number (1 for mono, 2 for second channel) tomwalters@0: for j = 1:Nstages % stage number tomwalters@0: jj = j + (k-1)*Nstages; % index into state columns tomwalters@0: %spatial smoothing: tomwalters@0: agcavg = filter(spacecoeffs, 1, [agcstate(1,jj); agcstate(:,jj); ... tomwalters@0: agcstate(Nch,jj)]); tomwalters@0: agcavg = agcavg(3:(Nch+2)); tomwalters@0: %time smoothing: tomwalters@0: epsilon = agcepsilons(j); tomwalters@0: agcstate(:,jj) = agcavg*(1-epsilon) + epsilon*detect(:,k)*agcgains(j); tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: %agcstate can't exceed 0.25, usually , with max detect being 0.5 tomwalters@0: % only INCREASE the damping over pdamp tomwalters@0: %agcfactor = 12; % 6.0; % 12.0; tomwalters@0: % now set above or as an argument tomwalters@0: tomwalters@0: offset = 1-agcfactor*DetectFun(0.0); % 0.7422 for DetectFun(0)=0.0215 tomwalters@0: % this makes the minimum damping (with 0 signal into AGC and agcstate being tomwalters@0: % equal to the DetectFun value at zero, which may be zero) equal the nominal tomwalters@0: tomwalters@0: for k = 1:Ntracks % track number (1 for mono, 2 for second channel) tomwalters@0: % pdampsmod(:,k) = pdamps.*min(1.5,(offset+agcfactor*... tomwalters@0: % (mean(agcstate')' + mean(agcstate(:,((k-1)*Nstages+1):k*Nstages)')')/2)); tomwalters@0: pdampsmod(:,k) = pdamps.*(offset+agcfactor*... tomwalters@0: (mean(agcstate')' + mean(agcstate(:,((k-1)*Nstages+1):k*Nstages)')')/2); tomwalters@0: % the above hack weights the track AGC and the average equally tomwalters@0: % for mono is same as just ...ofset+agcfactor*sum(agcstates')' tomwalters@0: end tomwalters@0: tomwalters@0: %plot([detect, pdamps, pdampsmod, agcstate, sum(agcstate')']) tomwalters@0: %drawnow