How to create a WAV file with a custom frequency tone / wave?

I have a problem with my wave generator. I am trying to create a wav file with sound of a given frequency. The code I'm using is:

$freqOfTone = 21000; $sampleRate = 44100; $samplesCount = 80000; $amplitude = 0.25 * 32768; $w = 2 * pi() * $freqOfTone / $sampleRate; for ($n = 0; $n < $samplesCount; $n++) { $data->samples[1][] = 32768 + (int)($amplitude * sin($n * $w)); } 

Unfortunately, the output wave is incorrect, I get several frequencies instead of one: http://i49.tinypic.com/ab1nx0.png

It should look like this: http://i50.tinypic.com/33zbslk.png

Where am I doing something wrong ?: (

+4
source share
2 answers

Given this sampling rate, the desired frequency is too close to the Nyquist frequency for proper sampling. I recommend that you use a sampling frequency of 96 kHz.

+1
source

I have achieved something final, but my decision is slightly different from this, which I had in mind ... Anyway, I took samples and put them through the hi-pass frequency filter. The signal is a bit distorted, but good enough for my purpose :)

0
source

Source: https://habr.com/ru/post/1438167/


All Articles