博客
关于我
离散波形的产生方法--正弦、余弦、三角波、方波、锯齿波
阅读量:535 次
发布时间:2019-03-07

本文共 2096 字,大约阅读时间需要 6 分钟。

生成波形函数

以下是一些常用的波形生成函数,适用于多种应用场景。这些函数可以根据需求进行参数调整,生成不同类型的波形。

1. 生成正弦波

int GenSineWave(int numElements, float64 amplitude, float64 frequency, float64 *phase, float64 *sineWave) {    int i = 1;    for(; i <= numElements; ++i) {        sineWave[i-1] = amplitude * sin(PI/180.0 * (*phase + 360.0 * frequency * i));    }    *phase = fmod(*phase + frequency * 360.0 * numElements, 360.0);    return 0;}

注意frequency = 1 / numElements

2. 生成方波

int GenSquareWave(int numElements, float64 amplitude, float64 frequency, float64 *phase, float64 dutyCycle, float64 *squareWave) {    int i = 0;    for(; i < numElements; ++i) {        float64 phase_i = fmod(*phase + 360.0 * frequency * i, 360.0);        squareWave[i] = (phase_i / 360.0) <= dutyCycle / 100.0 ? amplitude : -amplitude;    }    *phase = fmod(*phase + frequency * 360.0 * numElements, 360.0);    return 0;}

3. 生成锯齿波

int GenSawtoothWave(int numElements, float64 amplitude, float64 frequency, float64 *phase, float64 *sawtoothWave) {    int i = 0;    for(; i < numElements; ++i) {        float64 phase_i = fmod(*phase + 360.0 * frequency * i, 360.0);        float64 percentPeriod = phase_i / 360.0;        float64 dat = amplitude * 2.0 * percentPeriod;        sawtoothWave[i] = percentPeriod <= 0.5 ? dat : dat - 2.0 * amplitude;    }    *phase = fmod(*phase + frequency * 360.0 * numElements, 360.0);    return 0;}

4. 生成三角波

int GenTriangleWave(int numElements, float64 amplitude, float64 frequency, float64 *phase, float64 *triangleWave) {    int i = 0;    for(; i < numElements; ++i) {        float64 phase_i = fmod(*phase + 360.0 * frequency * i, 360.0);        float64 percentPeriod = phase_i / 360.0;        float64 dat = amplitude * 4.0 * percentPeriod;        if (percentPeriod <= 0.25) {            triangleWave[i] = dat;        } else if (percentPeriod <= 0.75) {            triangleWave[i] = 2.0 * amplitude - dat;        } else {            triangleWave[i] = dat - 4.0 * amplitude;        }    }    *phase = fmod(*phase + frequency * 360.0 * numElements, 360.0);    return 0;}

这些函数可以根据具体需求进行调整,例如改变频率、幅度和相位等参数,生成不同类型的波形。确保输入参数合理,避免超出范围。

转载地址:http://hpwnz.baihongyu.com/

你可能感兴趣的文章
python datetime笔记
查看>>
python day10
查看>>
python file
查看>>
Python FileDialog获取文件夹路径不是文件
查看>>
python filter过滤器的使用_python基础知识分享:zip()、filter函数和reduce如何使用?...
查看>>
python flask 请求code 400, message Bad request version
查看>>
Python Flink Stateful函数入口上的Kafka键访问
查看>>
Python float - str - 浮动怪异
查看>>
Python Flower库:分布式任务管理与监控
查看>>
Python for 循环和迭代器行为
查看>>
Python For循环多次返回
查看>>
Python ftplib - 指定端口
查看>>
Python furl库:一键搞定复杂URL操作
查看>>
Python GC 也会关闭文件吗?
查看>>
python gen_key.py 报错 提示找不到OpenSSL lib
查看>>
python getattrribute_Python学习——面向对象高级之反射
查看>>
Python进阶语法:字典推导式
查看>>
python gil_Python中GIL的使用详解
查看>>
python glob.glob使用
查看>>
python glob的安装和使用
查看>>