1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <iostream> #include <vector> #include <cmath> using namespace std; // Function to generate a sine wave signal vector<double> generateSineWave(double frequency, double sampleRate, int numSamples) { vector<double> signal(numSamples); double angularFrequency = 2.0 * M_PI * frequency; for (int i = 0; i < numSamples; ++i) { signal[i] = sin(angularFrequency * i / sampleRate); } return signal; } // Function to apply a simple low-pass filter vector<double> lowPassFilter(const vector<double>& signal, double alpha) { vector<double> filteredSignal(signal.size()); filteredSignal[0] = signal[0]; for (size_t i = 1; i < signal.size(); ++i) { filteredSignal[i] = alpha * signal[i] + (1 - alpha) * filteredSignal[i - 1]; } return filteredSignal; } // Function to print the signal void printSignal(const vector<double>& signal) { for (double sample : signal) { cout << sample << endl; } } int main() { double frequency = 5.0; // Frequency of the sine wave (Hz) double sampleRate = 100.0; // Sample rate (samples per second) int numSamples = 100; // Number of samples // Generate a sine wave vector<double> sineWave = generateSineWave(frequency, sampleRate, numSamples); // Apply a low-pass filter double alpha = 0.1; // Smoothing factor for the low-pass filter vector<double> filteredSignal = lowPassFilter(sineWave, alpha); // Print the original and filtered signals cout << "Original Sine Wave:" << endl; printSignal(sineWave); cout << "\nFiltered Signal:" << endl; printSignal(filteredSignal); return 0; } |
Explanation:
- Sine Wave Generation (
generateSineWave
):- The
generateSineWave
function generates a sine wave signal based on a specified frequency, sample rate, and number of samples. - It calculates each sample using the sine function,
sin(2 * pi * frequency * i / sampleRate)
, wherei
is the sample index. - The function returns a vector containing the generated sine wave samples.
Advertisement - The
- Low-Pass Filter (
lowPassFilter
):- The
lowPassFilter
function applies a simple first-order low-pass filter to the input signal. - The filter smooths out the signal by averaging each sample with the previous one, weighted by a smoothing factor
alpha
. - The parameter
alpha
controls the cutoff frequency of the filter; smaller values ofalpha
result in more smoothing (i.e., a lower cutoff frequency). - The filtered signal is returned as a vector of samples.
- The
- Signal Printing (
printSignal
):- The
printSignal
function prints the values of the signal samples to the console, allowing you to observe the waveform.
- The
- Main Function (
main
):- The
main
function sets up the parameters for generating a sine wave: the frequency (5 Hz), sample rate (100 samples per second), and number of samples (100). - It calls
generateSineWave
to produce the sine wave, then applies thelowPassFilter
with analpha
value of 0.1. - The original and filtered signals are printed to the console.
Advertisement - The
Possible Enhancements:
- Visualization: Integrate a graphical library like Matplotlib (using C++ bindings) to visualize the waveforms.
- Multiple Filters: Implement additional filter types (e.g., high-pass, band-pass) and compare their effects on the signal.
- Fourier Transform: Add functionality to perform a Fourier Transform (FFT) to analyze the frequency components of the signal.
- Noise Addition: Introduce noise to the signal and apply filtering to demonstrate noise reduction.