#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*** function declare ***/
void showSignal(double *arr, unsigned int count);
double* signal1(unsigned int* count);
double* signal2(unsigned int* count);
double* signal3(unsigned int* count);
double* (*fnTbl[4])(unsigned int*) = { signal1, signal2,signal3, };
/***function body***/
void main()
{
int stype = 0;
do {
printf("select signal type[0~2]:");
scanf("%d", &stype);
if (stype >= 0 && stype < 3) break;
printf("bad type, re");
} while (1);
unsigned int count = 0;
double* arr = fnTbl[stype](&count);
// r_noise(arr, count);
showSignal(arr, count);
free(arr);
}
const double cPI = 3.1415926;
double* signal1(unsigned int* count)
{
double fm = 25;
double t = 0.0;
const double timeDelta = 0.02;
double *arr = (double *)malloc(sizeof(double) * 256);
for (int i = -128; i < 128; ++i, t += timeDelta) {
const double dk = (cPI*fm*t);
const double dk2 = dk;
arr[i] = exp(-dk2) *(1.0 - 2.0*dk2);
}
*count = 256u;
return arr;
}
double* signal2(unsigned int* count)
{
unsigned int c = 40u;
*count = c;
double *arr = (double *)malloc(sizeof(double)* c);
for (const double dk = cPI / 20.0; c--;) {
arr[c] = sin(dk*(double)c);
}
return arr;
}
double* signal3(unsigned int* count)
{
const double f0 = 2.0;
const double fk = 5.0;
const double timeLen = 3.0;
const double timeDelta = 0.01;
const int sampleLen = (int)(timeLen / timeDelta);
double *arr = (double *)malloc(sizeof(double)* sampleLen);
double t = 0.0;
for (int i = 0; i < sampleLen; ++i, t += timeDelta) {
const double dk = 0.1*f0*t + fk*t*t;
arr[i] = cos(cPI*dk);
}
*count = (unsigned int)sampleLen;
return arr;
}
void showSignal(double *arr, unsigned int count)
{
//note what
}