0

I've been told to use a digital filter, but this solution achieves what I've been told to do. It is so simple it is dumb, what am I missing here? I want to be more scientific about it.

from matplotlib import pyplot as plt
import numpy as np

Set the amplitude of the signal

amplitude = 220

Set the frequency of the signal

frequency = 60.0

Calculate the period of the signal

period = 1 / frequency

Create an array of time values from 0 to 4 times the period with a step of 0.0001

time = np.arange(0, 4 * period, 0.0001)

Generate a sinusoidal signal using the given amplitude, frequency, and time values

signal = amplitude * np.sin(2 * np.pi * frequency * time)

Calculate harmonic components for the signal with different frequencies

h2 = (amplitude / 2) * np.sin(2 * np.pi * 2 * frequency * time) h3 = (amplitude / 3) * np.sin(2 * np.pi * 3 * frequency * time) h4 = (amplitude / 4) * np.sin(2 * np.pi * 4 * frequency * time) h6 = (amplitude / 6) * np.sin(2 * np.pi * 6 * frequency * time) h8 = (amplitude / 8) * np.sin(2 * np.pi * 8 * frequency * time)

Combine the original signal with its harmonic components

signal_with_harmonics = signal + h2 + h3 + h4 + h6 + h8

Extract the harmonic component from the signal

harmonic = signal_with_harmonics - signal

Apply a filter to the signal by removing the harmonic component

filtered_signal = signal_with_harmonics - harmonic

Create a plot for the signal with harmonics

ax = plt.axes() plt.title("Signal with Harmonics") plt.plot(time, signal_with_harmonics, c="red") plt.yticks(np.arange(-400, 400, 100)) plt.xlabel("Time(s)") plt.ylabel("Amplitude(v)") plt.show()

Create a plot for the harmonic component

ax = plt.axes() plt.title("Harmonic Component") plt.plot(time, harmonic, c="red") plt.yticks(np.arange(-400, 400, 100)) plt.xlabel("Time(s)") plt.ylabel("Amplitude(v)") plt.show()

Create a plot for the signal with the harmonic component removed (filtered signal)

ax = plt.axes() plt.title("Signal with Filter") plt.plot(time, filtered_signal, c="black") plt.yticks(np.arange(-400, 400, 100)) plt.xlabel("Time(s)") plt.ylabel("Amplitude(v)") plt.show()

Marcus Müller
  • 30,525
  • 4
  • 34
  • 58
kovyakov
  • 1
  • 1
  • you're subtracting the harmonics, because you know the harmonics, including their exact amplitude and phase. From where would a receiver of signal_with_harmonics know them? Or, if they knew them, why would it have to reconstruct the sine wave? it could just as well generate its own sine wave and ignore the received signal_with_harmonics altogether. – Marcus Müller Sep 19 '23 at 14:41
  • a) you are not applying a digital filter, you are just adding and subtracting harmonics. b) you plot into three different axes but they are all siting on tip of each other. Use subplot() or plot everything into the same axes. c) other than that your code is fine, why do you think it's wrong? – Hilmar Sep 19 '23 at 14:43

0 Answers0