Context-detection API for Android developed as a university project
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SingleSeriesFeature.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.dsp.extractor;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. /**
  26. *
  27. * @author chris
  28. */
  29. public abstract class SingleSeriesFeature implements Feature {
  30. private final int series;
  31. public SingleSeriesFeature(final int series) {
  32. this.series = series;
  33. }
  34. protected abstract String getFeatureName();
  35. public String getName() {
  36. return getFeatureName() + " (series " + series + ")";
  37. }
  38. public float getValue(final Window window) {
  39. final float[] values = new float[window.getData().size()];
  40. int i = 0;
  41. for (float[] set : window.getData()) {
  42. if (set.length <= series) {
  43. return Float.NaN;
  44. }
  45. values[i++] = set[series];
  46. }
  47. return getValue(values);
  48. }
  49. protected abstract float getValue(final float[] values);
  50. public static Set<Feature> createFeatures(
  51. final Class<? extends SingleSeriesFeature> type, final int number) {
  52. final Set<Feature> res = new HashSet<Feature>(number);
  53. for (int i = 0; i < number; i++) {
  54. try {
  55. res.add((Feature) type.getConstructor(Integer.TYPE).newInstance(i));
  56. } catch (Exception ex) {
  57. // Don't really care
  58. }
  59. }
  60. return res;
  61. }
  62. }