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()
This is the definitive code:
A main() to test it: