Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <time.h>
- #define BUF_SIZE 1211
- static int16_t buf[BUF_SIZE];
- static const int16_t a_sin = 2048;
- static const int fs = 200e3; /* sampling rate, samples/sec */
- static const int16_t dc = 100;
- static const double k_sin_avg = 0.637; /* A_avg/A_peak for sine wave */
- /**
- * Calculate the magnitude of sine wave signal (generated internally).
- *
- * @param f_sin Sine wave frequency, Hz
- * @param theta Start phase, radians
- *
- * @return Calculated A_peak
- */
- int calc(int f_sin, double theta)
- {
- size_t n;
- double sum = 0;
- double ppp; /* points per period */
- double fp; /* full periods in buffer */
- size_t poi; /* number of points of interest */
- int a_avg, a_peak;
- for (n = 0; n < BUF_SIZE; ++n)
- buf[n] = dc + a_sin * (1 + sin(2*M_PI*f_sin/fs * n + theta));
- /* Remove DC component first (using MEAN_BUF in fw) */
- for (n = 0; n < BUF_SIZE; ++n)
- buf[n] -= dc + a_sin;
- /* Calculate samples number for whole periods in the buffer */
- ppp = fs/f_sin;
- fp = floor((double)BUF_SIZE / ppp);
- poi = round(fp * ppp);
- /* Calculate the average and peak magnitude */
- for (n = 0; n < poi; ++n)
- sum += abs(buf[n]);
- a_avg = round(sum / poi);
- a_peak = round(a_avg / k_sin_avg);
- return a_peak;
- }
- int main(void)
- {
- int f;
- srand(time(0));
- for (f = 3000; f < 20000; ++f) {
- int a_peak;
- double err;
- a_peak = calc(f, rand());
- err = fabs(a_sin - a_peak) / a_sin * 100;
- if (err >= 1)
- printf("%d,%f\n", f, err);
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement