samer@0: /* samer@0: * Copyright (c) 2000, 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.units; samer@0: import samer.core.*; samer@0: import samer.core.types.*; samer@0: import samer.tools.*; samer@0: import samer.maths.*; samer@0: import java.util.*; samer@0: samer@0: /** samer@0: Temporally smoothed thresholding latch samer@0: */ samer@0: samer@0: public class Latch extends NamedTask samer@0: { samer@0: VDouble thresh; samer@0: VInteger countThresh; samer@0: boolean [] states; samer@0: int [] counts; samer@0: VVector output; samer@0: double[] in,out; samer@0: int n; samer@0: samer@0: public Latch(VVector input) samer@0: { samer@0: super("latch"); samer@0: n = input.size(); samer@0: states = new boolean[n]; samer@0: counts = new int[n]; samer@0: Shell.push(node); samer@0: thresh = new VDouble("thresh",0.5); samer@0: countThresh = new VInteger("count_thresh",3); samer@0: output = new VVector("output", n); samer@0: Shell.pop(); samer@0: samer@0: in=input.array(); samer@0: out=output.array(); samer@0: } samer@0: samer@0: public VVector output() { return output; } samer@0: samer@0: public void run() samer@0: { samer@0: for (int i=0; ithresh.value) { samer@0: if (++counts[i] >= countThresh.value) { samer@0: out[i]=in[i]; samer@0: states[i]=true; samer@0: counts[i]=0; samer@0: } samer@0: } else counts[i]=0; samer@0: samer@0: } else { samer@0: samer@0: // note is on - see about switching it off samer@0: if (in[i]<=0.8*thresh.value) { samer@0: if (++counts[i] >= countThresh.value) { samer@0: out[i]=-1; samer@0: states[i]=false; samer@0: counts[i]=0; samer@0: } else out[i]=in[i]; samer@0: } else { samer@0: counts[i]=0; samer@0: out[i]=in[i]; samer@0: } samer@0: } samer@0: } samer@0: output.changed(); samer@0: } samer@0: }