To construct a periodic signal with the given frequencies, you can use the following formula:
```c
#include <math.h>
#define PI 3.14159265358979323846
#define N 1024 // number of samples
double signal[N];
double t[N]; // time array
double dt = 20.48 / N; // time step
double fs = 50; // sampling frequency
for (int i = 0; i < N; i++) {
t[i] = i * dt;
signal[i] = sin(2 * PI * 0.1 * t[i]) + sin(2 * PI * 2 * t[i]) + sin(2 * PI * 4 * t[i]) + sin(2 * PI * 12 * t[i]) + sin(2 * PI * 18.3 * t[i]);
}
```
This code creates an array `signal` of length `N` that contains the samples of the periodic signal with the given frequencies. The time array `t` is also created, with a time step `dt` calculated based on the total time range and the number of samples.
To remove all frequencies outside the range 0.5 – 5 Hz from the signal, you can use a digital bandpass filter. One way to implement such a filter in C is to use the [Butterworth filter](
https://en.wikipedia.org/wiki/Butterworth_filter) design. The Butterworth filter has a maximally flat frequency response in the passband and a smooth transition to the stopband.
Here's an example implementation of a Butterworth bandpass filter in C:
```c
#include <math.h>
#define PI 3.14159265358979323846
void butterworthBandpass(int order, double* signal, int size, double dt, double lowcut, double highcut) {
int n = order / 2;
double* a = (double*)calloc(n + 1, sizeof(double));
double* d1 = (double*)calloc(n, sizeof(double));
double* d2 = (double*)calloc(n, sizeof(double));
double* w0 = (double*)calloc(n, sizeof(double));
double* w1 = (double*)calloc(n, sizeof(double));
double* w2 = (double*)calloc(n, sizeof(double));
double tanw0 = tan(PI * lowcut * dt);
double tanw1 = tan(PI * highcut * dt);
double bandwidth = log(tanw1 / tanw0);
double gain = pow(tanw0 * tanw1, n);
for (int i = 0; i < n; ++i) {
double parg = PI * (double)(2 * i + 1) / (double)(2 * n);
double aRe = sinh(bandwidth / 2) * cos(parg);
double aIm = cosh(bandwidth / 2) * sin(parg);
double realPart = pow(aRe, 2) - pow(aIm, 2);
double imagPart = 2.0 * aRe * aIm;
d1[i] = -2.0 / dt * realPart;
d2[i] = pow(1.0 / dt, 2) * realPart - pow(aIm, 2);
w0[i] = gain;
gain *= tanw0 / tanw1;
}
for (int j = n - 1; j >= 0; --j) {
for (int k = j; k >= 0; --k) {
w0[k] -= d1[j] * w1[k] + d2[j] * w2[k];
if (k > 0)
w2[k] = w2[k - 1];
else
w2[k] = 0.0;
w1[k] += w1[k + 1];
}
}
for (int i = n; i >= 0; --i)
a[i] = w0[i];
for (int i = size - 1; i >= order; --i) {
signal[i] -= d1[0] * signal[i - 1] + d2[0] * signal[i - 2];
for (int j = n - 1; j >= 1; --j)
signal[i] -= d1[j] * signal[i - j - 1] + d2[j] * signal[i - j - 2];
for (int j = order - 1; j >= n; --j)
signal[i] -= a[j + 1 - n] * signal[i - j - 1];
signal[i] /= a[0];
}
free(a);
free(d1);
free(d2);
free(w0);
free(w1);
free(w2);
}
```
This function takes as input the filter order `order`, a pointer to the signal array `signal`, the size of the signal array `size`, the time step `dt`, and the low and high cutoff frequencies `lowcut` and `highcut`. The function modifies the signal array in place to apply the bandpass filter.
You can use this function to filter the signal with different filter orders, such as 2, 4, and 6, and compare the results. For example:
```c
butterworthBandpass(2, signal, N, dt, 0.5, 5);
butterworthBandpass(4, signal, N, dt, 0.5, 5);
butterworthBandpass(6, signal, N, dt, 0.5, 5);
```
I hope this helps! Let me know if you have any further questions.