0

I am trying to modify the values of my X axis when plotting with AndroidPlot. This is because I am trying to represent the FFT of an audio spectrum and I would like the X axis to show the different frequencies. In the documentation is it recomended to use:

SimpleXYSeries.ArrayFormat.Y_VALS_ONLY

which uses the element index as the x value, but this is not what I need.

I need to do the following operation to convert the axis f = i * Fs / N being Fs, the sample rate in Hz and N, the number of points in the FFT.

I know that I need to create a new formatter and I have already check several examples like this,

this.getGraphWidget().setDomainValueFormat(new GraphXLabelFormat());

// ...

private class GraphXLabelFormat extends Format {

private static LABELS = ["Label 1", "Label 2", "Label 3"];

@Override
public StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) {
    int parsedInt = Math.round(Float.parseFloat(object.toString()));
    String labelString = = LABELS[parsedInt];

    buffer.append(labelString);
    return buffer;
}

@Override
public Object parseObject(String string, ParsePosition position) {
    return java.util.Arrays.asList(LABELS).indexOf(string);
}

}

The problem is that I dont really understand how it works and I don“t know how to adapt it to my necesities.

Thanks in advance!

paviflo
  • 105
  • 2
  • 13

1 Answers1

0

By default, the "object" field of your custom Format's format(Object, StringBuffer, FieldPosition) method corresponds to 'i' in your equation. You should therefore be able to do something like this:

@Override
public StringBuffer format(Object object, StringBuffer buffer, FieldPosition field) {
    int i = Math.round(Float.parseFloat(object.toString()));

    // assumes 'freqHz' and 'samples' (your array or list of of underlying samples) is
    // within scope of this method call:
    String thisFreq = i * (freqHz / samples.length);
    buffer.append(thisFreq + "Hz");
    return buffer;
}
Nick
  • 8,181
  • 4
  • 38
  • 63