Onda quadra

///Generatore di onde quadre

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int help(char **argv){
cout << "*** Square function generator ****\n";
cout << "Usage: " << argv[0] << " -Nn -f -A -B -d -R" << endl;
cout << "Where" << " -N:Num. of Points[R/f] -f:freq[1000Hz] -A:ampl1[1] -B:ampl2[0] -d:duty cycle[0.5] -R:sample freq[16K]" << endl;
return 1;
}
int main(int argc,char **argv)
{
int i, N = -1;
float t =0., A = 1., B=0, f = 1000, R = 16*1024, Ts = 1./R, pi = 4*atan(1.), dc = 0.5;

for (i =1; i < argc; i++)
{
if (('-' == argv[i][0]) || ('/' == argv[i][0]))
{
char cp;
cp = argv[i][1];
switch(cp)
{
case 'N':
N = atoi(argv[i]+2);
break;
case 'f':
f = atof(argv[i]+2);
break;
case 'A':
A = atof(argv[i]+2);
break;
case 'B':
B = atof(argv[i]+2);
break;
case 'd':
dc = atof(argv[i]+2);
break;
case 'R':
R = atof(argv[i]+2);
break;
case '?':
return help(argv);
break;
}
}
}

int samplesPerPeriod=0;
if(0!=f)
{
if (N < 0)
N=R/f;
samplesPerPeriod = int(R / (f));
}
else
samplesPerPeriod = N;
int firstLength = int(dc * samplesPerPeriod);
for (i =0; i < N; i++)
{
int k = i % samplesPerPeriod;
cout << (k < firstLength ? A : B) << endl;
}
return 0;
}

Ultime modifiche: venerdì, 27 settembre 2019, 19:22