波形发生器程序
时间: 2023-08-01 10:14:13 浏览: 29
好的,以下是一个简单的波形发生器程序,可以生成正弦波、方波和三角波:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main()
{
int type;
double frequency, amplitude, time, waveform;
int samplingRate, duration, i;
printf("Enter the type of waveform (1=sine, 2=square, 3=triangle): ");
scanf("%d", &type);
printf("Enter the frequency in Hz: ");
scanf("%lf", &frequency);
printf("Enter the amplitude in volts: ");
scanf("%lf", &litude);
printf("Enter the sampling rate in Hz: ");
scanf("%d", &samplingRate);
printf("Enter the duration in seconds: ");
scanf("%d", &duration);
for(i = 0; i < samplingRate * duration; i++)
{
time = (double)i / samplingRate;
switch(type)
{
case 1: //sine wave
waveform = amplitude * sin(2 * PI * frequency * time);
break;
case 2: //square wave
waveform = (fmod(2 * frequency * time, 1) < 0.5) ? amplitude : -amplitude;
break;
case 3: //triangle wave
waveform = 2 * amplitude / PI * asin(sin(2 * PI * frequency * time));
break;
default:
printf("Invalid waveform type.\n");
return 1;
}
printf("%f\n", waveform);
}
return 0;
}
```
该程序要求用户输入波形类型、频率、幅度、采样率和持续时间,并使用相应的函数生成波形数据。程序将波形数据逐行输出到控制台。
请注意,此程序并不考虑输出波形数据到设备的实现,如何将波形数据输出到设备将取决于您的具体应用场景。