博客
关于我
离散波形的产生方法--正弦、余弦、三角波、方波、锯齿波
阅读量: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/

你可能感兴趣的文章
project打开文件时,显示无法识别此文件格式?
查看>>
Prometheus + Grafana on Kubernetes部署
查看>>
prometheus + grafana进行服务器资源监控
查看>>
Prometheus Alertmanager 告警配置详解
查看>>
Prometheus Grafana 展示平台
查看>>
Prometheus pushgateway使用详解
查看>>
Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
查看>>
Prometheus 云原生 - 基于 file_sd、http_sd 实现 Service Discovery
查看>>
Prometheus 云原生 - 微服务监控报警系统 (Promethus、Grafana、Node_Exporter)部署、简单使用
查看>>
Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
查看>>
Prometheus 介绍
查看>>
Prometheus 安全配置详解
查看>>
prometheus 安装node_exporter, node_exporter 安装最新版 普罗米修思安装监控服务器client
查看>>
Prometheus 安装部署实战
查看>>
prometheus 持久化存储方案
查看>>
Prometheus 监控系统企业级实战
查看>>
Prometheus 监控系统简介
查看>>
Prometheus 配置详解
查看>>
Prometheus 采集器使用详解
查看>>
Prometheus 黑盒监控实战
查看>>