平均值,RMS和过零数字c ++数组

时间:2015-03-24 19:32:20

标签: c++ arrays function

我的任务是找到不同波形的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
};
// calculate and display all the average values //
cout << "Average value in cosine waveform : " << findAverage (cosine, SIZE1) << endl;

cout << "Average value of triangle waveform : " << findAverage (triangle, SIZE2) << endl;

cout << "Average value of sawtooth waveform : " << findAverage(sawtooth, SIZE3) << endl;

// calculate and display all the RMS values //
cout << "\nRMS value of cosine waveform : " << findRMS (cosine, SIZE1) << endl;

cout << "RMS value of triangle waveform : " << findRMS (triangle, SIZE2) << endl;

cout << "RMS value of sawtooth waveform : " << findRMS (sawtooth, SIZE3) << endl;

// calculate and display number of zero crossings //
cout << "\nNumber of zero crossings of cosine waveform : " << findZeroCrossings(cosine, SIZE1) << endl;

cout << "Number of zero crossings of triangle waveform : " << findZeroCrossings(triangle, SIZE2) << endl;

cout << "Number of zero crossings of sawtooth waveform : " << findZeroCrossings(sawtooth, SIZE3) << endl;
 }

 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 i)
 {
 if ((array[i] >= 0) && (array[i + 1]) < 0)
 i++;
 if ((array[i] < 0) && (array[i + 1] >= 0))
 i++;
 return i;
 }

我的源代码没有从c ++返回任何问题,但它没有返回交叉数的正确结果。

我得到的交叉数量:80,62,29

有关如何解决此问题的任何想法?我将如何在c ++中纠正每个波形?

2 个答案:

答案 0 :(得分:2)

RMS的计算移到for循环之外。收集完所有正方形后计算它。

而不是:

for (i = 0; i < size; i++)
{
   sumsquared += array[i]*array[i];
   RMS = sqrt((double(1)/size)*(sumsquared));
   return RMS;   // This returns after getting the square of just the
                 // first term.
}

使用:

for (i = 0; i < size; i++)
{
    sumsquared += array[i]*array[i];
}
return sqrt((double(1)/size)*(sumsquared));

答案 1 :(得分:0)

零交叉功能的问题:

 unsigned findZeroCrossings(short array[], int i)
 {
     if ((array[i] >= 0) && (array[i + 1]) < 0)
         i++;
     if ((array[i] < 0) && (array[i + 1] >= 0))
         i++;
     return i;
 }

你这样称呼它:

findZeroCrossings(sawtooth, SIZE3)

那你做什么?你在第二个参数中获取数组的大小,然后在数组结束后检查零交叉,这是非常错误的。

例如,如果您有一个大小为1000的数组,那么您将返回1000或1001个零交叉,因为您返回的是您给出的大小加上可能为1(代码中为i)。< / p>

你应该做的是这样的事情:

 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;
 }

学习更好地使用循环,并区分不同的变量!