Exemplo n.º 1
0
  /**
   * Creates a frequency table. The number of items in each class is returned. Classes are defined
   * by the limit and resolution. E.g. for a limit of 1200 with a resolution of 400 there are 3
   * classes: [0-400[, [400-800[ and [800-1200[.
   *
   * @param values the data to distribute over the bins/classes.
   * @param classWidth the resolution or the with of the classes
   * @param start the starting value
   * @param stop the stopping value
   * @return The number of items in each class
   */
  public static Histogram createFrequencyTable(
      final List<Double> values, final double classWidth, final double start, final double stop) {
    final Histogram histogram = new Histogram(start, stop, (int) ((stop - start) / classWidth));
    for (final Double value : values) {
      histogram.add(value);
    }
    assert histogram.getSumFreq() == values.size()
        : "Number of items in bins does no" + "t correspond with total number of items";

    return histogram;
  }
Exemplo n.º 2
0
  /**
   * Reads a frequency table (histogram) from disk. The source file is expected to be a CSV-file in
   * the format: <code>value;frequency[;other data; is discarded;...]</code> The lowest value is on
   * the first row, the highest on the last!
   *
   * @param fileName
   * @return a frequencytable
   */
  public static Histogram readFrequencyTable(final String fileName) {
    final List<String[]> data = FileUtils.readCSVFile(fileName, ";", -1);

    final double classWidth =
        Double.parseDouble(data.get(1)[0]) - Double.parseDouble(data.get(0)[0]);
    final double start = Double.parseDouble(data.get(0)[0]) - classWidth / 2.0;
    final double stop = Double.parseDouble(data.get(data.size() - 1)[0]) + classWidth / 2.0;

    final Histogram table = new Histogram(start, stop, (int) ((stop - start) / classWidth));
    for (final String[] row : data) {
      final int frequency = (int) Double.parseDouble(row[1]);
      final double value = Double.parseDouble(row[0]);
      for (int i = 0; i < frequency; i++) {
        table.add(value);
      }
    }
    return table;
  }
Exemplo n.º 3
0
 public static void exportFrequencyTable(final Histogram histogram, final String fileName) {
   exportFrequencyTable(histogram, fileName, histogram.getStart(), histogram.getStop());
 }
Exemplo n.º 4
0
  public static void exportFrequencyTable(
      final Histogram histogram, final String fileName, final double start, final double stop) {
    final StringBuilder sb = new StringBuilder();
    for (double current = start + histogram.getClassWidth() / 2;
        current <= stop;
        current += histogram.getClassWidth()) {
      final double count = histogram.getCount(current);
      final long cumFreq = histogram.getCumFreq(current);
      final double derivative =
          current + histogram.getClassWidth() > stop
              ? 0
              : (histogram.getCount(current)
                      - histogram.getCount(current + histogram.getClassWidth()))
                  / histogram.getClassWidth();

      double psd = 0.0;
      if (current + 2 * histogram.getClassWidth() <= stop) {
        psd =
            (histogram.getCount(current + 2 * histogram.getClassWidth())
                    - histogram.getCount(current))
                / (2 * histogram.getClassWidth());
      }
      // double derivative = (current + frequencyTable.getClassWidth() <=
      // stop)? 0 : (frequencyTable.getCount(current +
      // frequencyTable.getClassWidth()) -
      // frequencyTable.getCount(current))
      // /frequencyTable.getClassWidth();
      sb.append(current)
          .append(";")
          .append(count)
          .append(";")
          .append(cumFreq)
          .append(";")
          .append(derivative)
          .append(";")
          .append(psd)
          .append("\n");
    }
    FileUtils.writeFile(sb.toString(), fileName);

    final Plot h = new Plot();
    h.setXRange(start, stop);

    boolean first = true;

    double highWaterMark = 0;
    final double[] values = new double[histogram.getNumberOfClasses()];
    int i = 0;
    for (double current = start + histogram.getClassWidth() / 2;
        current <= stop;
        current += histogram.getClassWidth()) {

      h.addPoint(0, current, histogram.getCount(current), !first);
      values[i] = histogram.getCount(current);
      if (histogram.getCount(current) > highWaterMark) {
        highWaterMark = histogram.getCount(current);
      }
      i++;
      first = false;
    }

    /*
     * List<Peak> peaks = PeakDetector.peakDetection(histogram,fileName +
     * ".midi", start, stop); int peakIndex = 1; for(Peak peak:peaks){ int
     * position = peak.getPosition();
     * h.addPoint(1,peak.getLift(),peak.getLTop(),false);
     * h.addPoint(1,position,peak.getTop(),true);
     * h.addPoint(1,peak.getRight(),peak.getRTop(),true); peakIndex++; }
     */

    if (stop == 1200.0) {

      h.setXLabel("n (cents)");
      h.setYLabel("frequency of ocurrence");

      // h.addLegend(0,"Pitch histogram");

      for (int j = 0; j <= 1200; j += 100) {
        h.addXTick(j + "", j);
      }
      /*
       * h.addXTick("Fifth", reference - 700); h.addXTick("Fifth",
       * reference + 700); h.addXTick("Tritonus", reference - 600);
       * h.addXTick("Tritonus", reference + 600);
       * h.addXTick("Kleine terts",reference + 300);
       * h.addXTick("Kleine terts",reference - 300);
       * h.addXTick("Grote terts",reference + 400);
       * h.addXTick("Grote terts",reference - 400); h.setWrap(true);
       */

      // h.addYTick("Gem", histogram.getSumFreq() / (float)
      // histogram.getNumberOfClasses());
      // h.addYTick("Med", StatUtils.percentile(values, 0.5));
      // h.setXRange(43, 1147);
    }

    h.setSize(1024, 786);

    h.setTitle(FileUtils.basename(fileName));

    try {
      Thread.sleep(60);
      final BufferedImage image = h.exportImage();
      ImageIO.write(image, "png", new File(fileName.substring(0, fileName.length() - 4) + ".png"));
    } catch (final IOException e) {
      e.printStackTrace();
    } catch (final InterruptedException e1) {
      e1.printStackTrace();
    }
  }