如何使用自定义频率音/波创建.wav文件?

时间:2022-12-15 19:45:22

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:

我的波浪发生器有问题。我正在尝试使用给定频率的声音创建.wav文件。我使用的代码:

$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

不幸的是,输出波不正确,我得到的频率很少而不是一个:http://i49.tinypic.com/ab1nx0.png

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

它应该是这样的:http://i50.tinypic.com/33zbslk.png

Where am I doing something wrong? :(

我在哪里做错了什么? :(

2 个解决方案

#1


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.

给定采样率,所需频率太接近奈奎斯特频率以便正确采样。我建议您使用96kHz的采样率。

#2


0  

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 :)

我已经取得了一些成果,但我的解决方案与我的意思略有不同......无论如何,我拿了样品并通过高通滤波器进行了测试。信号有点扭曲,但我的目的足够好:)

#1


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.

给定采样率,所需频率太接近奈奎斯特频率以便正确采样。我建议您使用96kHz的采样率。

#2


0  

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 :)

我已经取得了一些成果,但我的解决方案与我的意思略有不同......无论如何,我拿了样品并通过高通滤波器进行了测试。信号有点扭曲,但我的目的足够好:)