Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce harmonics generating a pure tone in Java

I'm trying to develop an static method in Java to generate a pure tone.

In the begining it seemed easy, but when I've try to write the double array to the loudspeakers I appreciate too much harmonics.

I test it with an spectrum analyzer (sonometer) and then, also I've drawn in a graphic the array resultant. When I've done it I've seen the problem:

It's about the wave form, it's abrupted. I want to smooth this array, but I don't know how to do it.

This is the code:

/**
 * Genera un tono puro.
 * @param bufferSize Tamaño del buffer.
 * @param fs Frecuencia de muestreo.
 * @param f0 Frecuencia central. 
 * @return El tono puro.
 */
public static double[] generateTone(int bufferSize, int fs, int f0) {
    double[] tone = new double[bufferSize]; // Tono
    double angle; // Ángulo del tono

    // Sólo hace falta recorrer la mitad del array, ya que hay simetría:
    for (int i = 0; i < tone.length / 2; i++) {
        angle = 2 * Math.PI * f0 * i / fs; // Calculamos la variación del ángulo

        // Tenemos que conseguir que la señal sea menos abrupta para reducir al máximo los armónicos):
        tone[2 * i + 1] = tone[2 * i] = Math.sin(angle); // Aprovechamos la simetría
    }

    return tone;
} // getSinus()
like image 521
Gabi Moreno Avatar asked Jan 24 '14 22:01

Gabi Moreno


2 Answers

Writing the same value to two consecutive locations introduces a step into the waveform. Any deviation from a smooth sine curve adds harmonics. If you want a pure tone, Don't Do That. If you want to do that, Don't Expect A Pure Tone.

like image 187
keshlam Avatar answered Sep 28 '22 04:09

keshlam


You need to compute the angle and sine for every value of 'bufferLength', not from every second value. What you're doing is essentially under-sampling with interpolation. I don't see any 'symmetry' about that.

like image 26
user207421 Avatar answered Sep 28 '22 02:09

user207421