三角波鋸齒波輸入 振幅,坡1寬,坡2寬,無波寬,時步
三角波 w1=w2, 鋸齒波 w1 != w2.
程序沒有考慮時間 寬度w1=0 或 w2 =0,如果等於0,請給壹個非0值,例如 0.0000001.
脈沖方波,請自己模仿三角波程序計算。w期間等於amp,flat_t期間等於0.
#include <stdio.h>
#include <math.h>
void main()
{
double y[2048];
double pi,r;
double amp,f,phi,dt,t,x;
double w,flat_t,slop1,slop2,L,w1,w2;
int flag_w1=0,flag_w2=0;
long int i;
int N=2048;
char str[5];
pi = atan(1.0) * 4.0;
fprintf(stderr,"Enter sin or tri\n");
scanf("%s",&str[0]);
if ( strncmp(str,"sin",3) == 0)
{
fprintf(stderr,"enter Amp Hz delta_t phase\n");
fprintf(stderr,"for example: 1.0 10.0 0.001 1.414\n");
scanf("%lf %lf %lf %lf",&,&f,&dt,&phi);
for (i=0;i<N;i++) y[i]= amp * sin(2.0*pi * f * dt * i + phi );
for (i=0;i<N;i++) fprintf(stdout,"%lf\n",y[i]);
exit(0);
};
fprintf(stderr,"enter Amp w1 w2 flat_t dt\n");
fprintf(stderr,"for example: 1. 1. 1. 1. 0.01 \n");
scanf("%lf %lf %lf %lf %lf",&,&w1,&w2,&flat_t,&dt);
w = w1+w2;
L = w + flat_t;
slop1 = amp / w1;
slop2 = -amp / w2;
for (i=0;i<N;i++)
{
t = dt * (double) i;
x = t - L * (double) ( (int) (t / L));
if (x >= 0.0 && x <= w1) y[i] = slop1 * x;
if (x > w1 && x < w ) y[i] = amp + slop2 * (x - w1);
if (x >= w && x <= L ) y[i] = 0.0;
}
for (i=0;i<N;i++) fprintf(stdout,"%lf\n",y[i]);
exit(0);
}