import numpy as np
import matplotlib.pyplot as plt
class EngineCurrentAnalyse1:
def __init__(self, fs=50000, base_freq=50, sample_len_in_s=1):
self.period_samples = fs//base_freq
self.fs = fs
self.base_freq = 50
self.sample_len_in_s = sample_len_in_s
def breakOneBar(self, t, final_signal):
processed_signal = np.copy(final_signal)
for i in range(0, len(t), self.period_samples):
start_idx = i
end_idx = i + int(self.period_samples / 6)
if end_idx > len(t):
break
cos_coeff = np.cos(2 * np.pi * self.base_freq*0.97 * t[start_idx:end_idx])
processed_signal[start_idx:end_idx] *= cos_coeff
return processed_signal
def GenCurrentData(self):
fs = self.fs
t = np.linspace(0, self.sample_len_in_s, fs, endpoint=False)
f_signal = self.base_freq
signal_50Hz = np.sin(2 * np.pi * f_signal * t)
noise = np.random.normal(0, 0.1, t.shape)
f_inverter = 2000
inverter_signal = np.sin(2 * np.pi * f_inverter * t)
inverter_noise = np.random.normal(0, 0.05, t.shape)
inverter_noisy_signal = inverter_signal + inverter_noise
final_signal = signal_50Hz + noise + inverter_noisy_signal
return t, final_signal, fs,
def draw_signal(self, final_signal, t, fs):
fourier_transform = np.fft.fft(final_signal)
freqs = np.fft.fftfreq(len(final_signal), 1/fs)
n = len(final_signal)
single_sided_fft = 2.0/n * np.abs(fourier_transform[:n//2])
single_sided_freqs = freqs[:n//2]
single_sided_fft_db = 20 * np.log10(single_sided_fft)
plt.figure(figsize=(12, 10))
plt.subplot(3, 1, 1)
plt.plot(t, final_signal, label='Final Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.title('Three - phase Motor Line Current Single - phase Waveform with Noise (Time Domain)')
plt.legend()
plt.grid(True)
plt.subplot(3, 1, 2)
plt.plot(single_sided_freqs, single_sided_fft_db, label='Frequency Domain (dB)')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')
plt.title('Three - phase Motor Line Current Single - phase Waveform with Noise (Frequency Domain in dB)')
plt.legend()
plt.grid(True)
span=0.25
left = (1-span)/2
bottom = 0.05
width = span
height = 0.2
ax = plt.axes([left, bottom, width, height])
ax.plot(single_sided_freqs, single_sided_fft_db, label='Frequency Domain (dB)')
ax.set_xlabel('Frequency (Hz)')
ax.set_ylabel('Magnitude (dB)')
ax.set_title('Three - phase Motor Line Current Single - phase Waveform with Noise (Frequency Domain in dB)')
ax.legend()
ax.set_xlim(0, 2 * self.base_freq)
ax.grid(True)
plt.grid(True)
plt.tight_layout()
plt.show()
eca = EngineCurrentAnalyse1()
t,sig,fs = eca.GenCurrentData()
sig = eca.breakOneBar(t, sig)
eca.draw_signal(sig, t, fs)