How create .wav file with a custom frequency tone / wave?

不羁的心 提交于 2019-12-10 09:07:54

问题


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

$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 few 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? :(


回答1:


Given that sample rate, the desired frequency is too close to the Nyquist frequency to be sampled properly. I recommend you use a sample rate of 96kHz for this.




回答2:


I have achieved something finaly, but my solution is a little bit different from this which I meant... Anyway, I took the samples and put them through hi-pass frequency filter. Signal is a little bit distorted, but stil good enough for my purpose :)



来源:https://stackoverflow.com/questions/12761094/how-create-wav-file-with-a-custom-frequency-tone-wave

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!