java - 正弦波在Java中交替失真

标签 java math signals trigonometry

我正在尝试生成正弦波并将其添加到字节数组。我搜索并找到了。但是,我总是像附件一样得到失真的波形。

请告诉我您的意见,为什么会这样。谢谢。

我的代码在这里

private byte[] getData(int freq) {   // taking pitch data
    double pha = Math.PI/2;          // defining phase
    final int LENGTH = 44100 * 10;   // defining length of sine wave, byte array
    final byte[] arr = new byte[LENGTH];
    for(int i = 0; i < arr.length; i++) {
        double angle = (2.0 * Math.PI * i*freq+pha) / (44100); 
        arr[i] = (byte) (Math.cos(angle) *127* 0.3);    // 0.3 is amplitude scale        
    }
    return arr;
}

Distort waveform example pic

enter image description here

最佳答案

代码看起来不错。我怀疑这是解释 two's complement 的可视化工具有符号值作为无符号(-1 变为 255-2 变为 254 等等)。

I write to a wav file and plot it with SonicVisualiser

根据 WAVE PCM soundfile format :

8-bit samples are stored as unsigned bytes, ranging from 0 to 255. 16-bit samples are stored as 2's-complement signed integers, ranging from -32768 to 32767.

看起来您要么需要将正弦波向上移动 128(以便它完全适合 0-255 范围内),要么转向使用 16 位样本。

关于java - 正弦波在Java中交替失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26632480/

相关文章:

linux - nanosleep() 系统调用因总线错误而醒来?

java - JPA 在创建后返回嵌套对象 - 500 内部服务器错误

java - 一定尺寸范围内的范围?

python - mousepress事件的问题

java - 比较一对 3 个变量的数学方法

algorithm - GPU 上的线性排序。并行处理会改变 Big-O 吗?

c - 使用 `SIGCHLD` 忽略 `sigaction(2)` 信号有什么用?

java - Spring 中的依赖注入(inject)

java - 在 GAE 中保留数据 - 实体不能有长主键并且不能是子对象

java - 并发和多线程有什么区别?