Mercurial > hg > jslab
annotate src/samer/units/Latch.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
rev | line source |
---|---|
samer@0 | 1 /* |
samer@0 | 2 * Copyright (c) 2000, Samer Abdallah, King's College London. |
samer@0 | 3 * All rights reserved. |
samer@0 | 4 * |
samer@0 | 5 * This software is provided AS iS and WITHOUT ANY WARRANTY; |
samer@0 | 6 * without even the implied warranty of MERCHANTABILITY or |
samer@0 | 7 * FITNESS FOR A PARTICULAR PURPOSE. |
samer@0 | 8 */ |
samer@0 | 9 |
samer@0 | 10 package samer.units; |
samer@0 | 11 import samer.core.*; |
samer@0 | 12 import samer.core.types.*; |
samer@0 | 13 import samer.tools.*; |
samer@0 | 14 import samer.maths.*; |
samer@0 | 15 import java.util.*; |
samer@0 | 16 |
samer@0 | 17 /** |
samer@0 | 18 Temporally smoothed thresholding latch |
samer@0 | 19 */ |
samer@0 | 20 |
samer@0 | 21 public class Latch extends NamedTask |
samer@0 | 22 { |
samer@0 | 23 VDouble thresh; |
samer@0 | 24 VInteger countThresh; |
samer@0 | 25 boolean [] states; |
samer@0 | 26 int [] counts; |
samer@0 | 27 VVector output; |
samer@0 | 28 double[] in,out; |
samer@0 | 29 int n; |
samer@0 | 30 |
samer@0 | 31 public Latch(VVector input) |
samer@0 | 32 { |
samer@0 | 33 super("latch"); |
samer@0 | 34 n = input.size(); |
samer@0 | 35 states = new boolean[n]; |
samer@0 | 36 counts = new int[n]; |
samer@0 | 37 Shell.push(node); |
samer@0 | 38 thresh = new VDouble("thresh",0.5); |
samer@0 | 39 countThresh = new VInteger("count_thresh",3); |
samer@0 | 40 output = new VVector("output", n); |
samer@0 | 41 Shell.pop(); |
samer@0 | 42 |
samer@0 | 43 in=input.array(); |
samer@0 | 44 out=output.array(); |
samer@0 | 45 } |
samer@0 | 46 |
samer@0 | 47 public VVector output() { return output; } |
samer@0 | 48 |
samer@0 | 49 public void run() |
samer@0 | 50 { |
samer@0 | 51 for (int i=0; i<n; i++) { |
samer@0 | 52 |
samer@0 | 53 if (!states[i]) { |
samer@0 | 54 |
samer@0 | 55 // unit is off - check to see if it should be on |
samer@0 | 56 if (in[i]>thresh.value) { |
samer@0 | 57 if (++counts[i] >= countThresh.value) { |
samer@0 | 58 out[i]=in[i]; |
samer@0 | 59 states[i]=true; |
samer@0 | 60 counts[i]=0; |
samer@0 | 61 } |
samer@0 | 62 } else counts[i]=0; |
samer@0 | 63 |
samer@0 | 64 } else { |
samer@0 | 65 |
samer@0 | 66 // note is on - see about switching it off |
samer@0 | 67 if (in[i]<=0.8*thresh.value) { |
samer@0 | 68 if (++counts[i] >= countThresh.value) { |
samer@0 | 69 out[i]=-1; |
samer@0 | 70 states[i]=false; |
samer@0 | 71 counts[i]=0; |
samer@0 | 72 } else out[i]=in[i]; |
samer@0 | 73 } else { |
samer@0 | 74 counts[i]=0; |
samer@0 | 75 out[i]=in[i]; |
samer@0 | 76 } |
samer@0 | 77 } |
samer@0 | 78 } |
samer@0 | 79 output.changed(); |
samer@0 | 80 } |
samer@0 | 81 } |