我的任务是针对不同的波形,在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 ++中的每个波形?
最佳答案
将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));
关于c++ - 平均数,均方根值和零交叉点数数组c++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29241290/