import pandas as pd import matplotlib.pyplot as plt from numpy import * def load_weights(filename): # Load filter weights from a CSV file without a header return pd.read_csv(filename, header=None, names=['weights']).iloc[:, 0].values def Anomaly_detection(y, mu, n, weights): # Initialization N = len(y) yn = zeros(N) # filter output, y with tilde nx = 1 + n # length of vector x=[x0=1 x1 ... xn] nw = nx # for linear neuron (filter) e = zeros(N) # neuron error e = y - yn w = random.randn(nw) / nw # weight initialization x = ones(nx) # x=[1 1 1 ...1] Wall = zeros((N + 1, nw)) Wall[:n, :] = w yn[:n] = y[:n] # initial conditions dWall = zeros((N, nw)) for k in range(n, N): x[1:] = y[k - n:k] # x=[x0=1 y(k-n) y(k-n-1) ... y(k-1)] yn[k] = dot(w, x) e[k] = y[k] - yn[k] dw = mu / (sum(x ** 2)) * e[k] * x w = w + dw dWall[k, :] = dw Wall[k, :] = w return e, yn, yn # Load and preprocess new data new_df = pd.read_csv('# Nacist File', header=None, names=['Column']) new_df_n = (new_df - mean(new_df))/3 # Load the trained weights from the CSV file without a header loaded_weights = load_weights('trained_weights_FFT.csv') # Learning points n_learn = 20 # Parameters for updating weights mu_update = 0.4 # Update weights based on the new data Anomaly_detection, prediction_error, filtered_signal = Anomaly_detection(new_df_n['Column'].values, mu_update, n_learn, loaded_weights) # Find the index where amplitude drops below 0.8 start_index = next(i for i, value in enumerate(new_df_n['Column'].values) if value < 10) # Print the updated weights print("Updated Weights:", Anomaly_detection) # Plotting Freq_start = 3450 Freq_end = 3850 plt.figure(figsize=(12, 10)) # Adjust the figure size # Plot original and filtered signals starting from the index where amplitude drops below 0.8 plt.subplot(3, 1, 1) # Increase the number of rows to accommodate the new subplot frequency_values = linspace(0, 20000, len(new_df_n['Column'].values)) # Assuming the x-axis corresponds to frequency values plt.plot(frequency_values[start_index:], new_df_n['Column'].values[start_index:], label='Original Signal', color='blue') plt.plot(frequency_values[start_index:], filtered_signal[start_index:], label='Filtered Signal', color='green') plt.axvline(x=Freq_start, color='orange', linestyle='--', label='Vertical Line at 3550') plt.axvline(x=Freq_end, color='purple', linestyle='--', label='Vertical Line at 3750') plt.xlabel('Frequency (Hz)') # Adjust x-axis label plt.ylabel('Amplitude') plt.legend() # Plot prediction error plt.subplot(3, 1, 2) # Increase the number of rows plt.plot(frequency_values[start_index:], prediction_error[start_index:], label='Prediction Error', color='red') plt.axhline(y=2, color='green', linestyle='--', label='Threshold') # Add threshold line plt.axvline(x=Freq_start, color='orange', linestyle='--', label='Vertical Line at 3550') plt.axvline(x=Freq_end, color='purple', linestyle='--', label='Vertical Line at 3750') plt.xlabel('Frequency (Hz)') # Adjust x-axis label plt.ylabel('Error') plt.legend() # Create a new subplot for the zoomed-in area plt.subplot(3, 1, 3) # Increase the number of rows plt.plot(frequency_values[start_index:], prediction_error[start_index:], label='Prediction Error', color='red') plt.axhline(y=2, color='green', linestyle='--', label='Threshold') # Add threshold line plt.axvline(x=Freq_start, color='orange', linestyle='--', label='Vertical Line at 3550') plt.axvline(x=Freq_end, color='purple', linestyle='--', label='Vertical Line at 3750') # Set the x-axis limit for the zoomed-in area plt.xlim(Freq_start, Freq_end) plt.ylim(0, 8) # Adjust the y-axis limit as needed # Add labels to the zoomed-in area plt.xlabel('Frequency (Hz)') plt.ylabel('Error') plt.legend() # Show the plot plt.tight_layout() plt.show() # Check if error exceeds the threshold in the area of 3000 to 4000 if any((3000 <= frequency_values) & (frequency_values <= 4000) & (abs(prediction_error) > 2)): print("Warning! - Knocking Expected")