samer@0: /* samer@0: * Copyright (c) 2002, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.models; samer@0: samer@0: import samer.core.*; samer@0: import samer.core.types.*; samer@0: import samer.maths.*; samer@0: import samer.maths.opt.*; samer@0: import samer.tools.*; samer@0: samer@0: /** samer@0: Automatic gain control for a given input vector. samer@0: Ouput appears in out. Also generates a trace of samer@0: 'loudness' of input signal. samer@0: */ samer@0: samer@0: public class Scaler extends NullTask implements Model samer@0: { samer@0: private Model M; samer@0: private int n; samer@0: private Vec x; samer@0: private VVector s; samer@0: private VDouble multiplier; samer@0: private VDouble offset; samer@0: private double logK; samer@0: samer@0: double [] _x, _s, _g, phi; samer@0: samer@0: public Scaler( Vec input, Model M) { this(input); setOutputModel(M); M.setInput(s); } samer@0: public Scaler( Vec input) { this(input.size()); setInput(input); } samer@0: public Scaler( int N) samer@0: { samer@0: n = N; samer@0: samer@0: x = null; samer@0: s = new VVector("output",n); samer@0: multiplier = new VDouble("scale",1.0,VDouble.SIGNAL); samer@0: offset = new VDouble("offset",0.0,VDouble.SIGNAL); samer@0: samer@0: _s = s.array(); samer@0: _g = new double[n]; samer@0: phi = null; samer@0: reset(); samer@0: logK=Math.log(multiplier.value); samer@0: } samer@0: samer@0: public int getSize() { return n; } samer@0: public VVector output() { return s; } samer@0: public VDouble getScale() { return multiplier; } samer@0: public VDouble getOffset() { return offset; } samer@0: public Model getOutputModel() { return M; } samer@0: public void setOutputModel(Model m) { M=m; } samer@0: public void setInput(Vec in) { x=in; _x=x.array(); } samer@0: public void reset() { samer@0: // multiplier.load(Shell.env()); samer@0: // offset.load(Shell.env()); samer@0: } samer@0: samer@0: public String toString() { return "Scaler:"+x; } // +"->"+s; } samer@0: public void dispose() samer@0: { samer@0: offset.dispose(); samer@0: multiplier.dispose(); samer@0: s.dispose(); samer@0: super.dispose(); samer@0: } samer@0: samer@0: public void infer() { samer@0: double a=offset.value, k=1/multiplier.value; samer@0: for (int i=0; i 8000) return; samer@0: double [] phi=M.getGradient(); samer@0: double g=0; samer@0: for (int i=0; i