我有三个不同波形的3个数组。
我需要纠正每个波形(所以负值变为它们的正当量),并使用3种不同的函数找到每个波形的平均值,RMS和过零次数。
据我所知,我的功能已经建立。
然而,如何纠正这些波形?
#include <iostream>
#include <cmath>
using namespace std;
#define SIZE1 80
#define SIZE2 62
#define SIZE3 29
double findAverage(short array[], int size);
double findRMS(short array[], int size);
unsigned findZeroCrossings (short array[], int size);
int main()
{
// array of values in cosine waveform //
short cosine[SIZE1] = {
32767, 32666, 32364, 31862, 31163, 30273, 29196, 27938, 26509, 24916,
23170, 21280, 19260, 17121, 14876, 12539, 10126, 7649, 5126, 2571,
0, -2571, -5126, -7649, -10126, -12539, -14876, -17121, -19260, -21280,
-23170, -24916, -26509, -27938, -29196, -30273, -31163, -31862, -32364, -32666,
-32767, -32666, -32364, -31862, -31163, -30273, -29196, -27938, -26509, -24916,
-23170, -21280, -19260, -17121, -14876, -12539, -10126, -7649, -5126, -2571,
0, 2571, 5126, 7649, 10126, 12539, 14876, 17121, 19260, 21280,
23170, 24916, 26509, 27938, 29196, 30273, 31163, 31862, 32364, 32666
};
// array of values in triangle waveform //
short triangle[SIZE2] = {
15500, 14500, 13500, 12500, 11500, 10500, 9500, 8500, 7500, 6500,
5500, 4500, 3500, 2500, 1500, 500, -500, -1500, -2500, -3500,
-4500, -5500, -6500, -7500, -8500, -9500, -10500, -11500, -12500, -13500,
-14500, -15500, -14500, -13500, -12500, -11500, -10500, -9500, -8500, -7500,
-6500, -5500, -4500, -3500, -2500, -1500, -500, 500, 1500, 2500,
3500, 4500, 5500, 6500, 7500, 8500, 9500, 10500, 11500, 12500,
13500, 14500
};
// array of values in sawtooth waveform //
short sawtooth[SIZE3] = {
-24000, -22000, -20000, -18000, -16000, -14000, -12000, -10000, -8000, -6000,
-4000, -2000, 0, 2000, 4000, 6000, 8000, 10000, 12000, 14000,
16000, 18000, 20000, 22000, 24000, 26000, 28000, 30000, 32000
};
// TODO
}
double findAverage(short array[], int size)
{
int i;
int sum = 0;
double average;
for (i = 0; i < size; ++i)
{
sum += array[i];
}
average = double(sum) / size;
return average;
}
double findRMS(short array[], int size)
{
int i;
double sumsquared;
double RMS;
sumsquared = 0;
for (i = 0; i < size; i++)
{
sumsquared += array[i]*array[i];
}
RMS = sqrt((double(1)/size)*(sumsquared));
return RMS;
}
unsigned findZeroCrossings(short array[], int size)
{
int zeroCrossings = 0;
for (int i = 0; i + 1 < size; i++)
{
if ((array[i] >= 0) && (array[i + 1]) < 0)
zeroCrossings++;
if ((array[i] < 0) && (array[i + 1] >= 0))
zeroCrossings++;
}
return zeroCrossings;
}
答案 0 :(得分:0)
如果您的纠正只是为了获得数据点的绝对值,那么只需重写它们。
这样的事情:
void rectify(short *array, int size)
{
int i;
for (i = 0; i < size; ++i)
if(array[i] < 0)
array[i] *= -1;
}
如何在此上下文中使用它。将这些放在main
函数的末尾:
printf("Avg of sawtooth: %f\n", findAverage(sawtooth, SIZE3));
rectify(sawtooth, SIZE3); // Note: the sawtooth array changed.
printf("Avg of rectified sawtooth: %f\n", findAverage(sawtooth, SIZE3));