Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from scipy.signal import butter, lfilter, cheby1
|
5 |
+
import plotly.graph_objects as go
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
|
8 |
+
# Function to apply FFT
|
9 |
+
def apply_fft(data, sampling_rate):
|
10 |
+
n = len(data)
|
11 |
+
fft_result = np.fft.fft(data)
|
12 |
+
freq = np.fft.fftfreq(n, 1 / sampling_rate)
|
13 |
+
|
14 |
+
|
15 |
+
return freq, np.abs(fft_result)
|
16 |
+
# Function to apply Wiener filter
|
17 |
+
def apply_wiener_filter(noisy_signal, noise_level):
|
18 |
+
signal_power = np.mean(noisy_signal ** 2)
|
19 |
+
noise_power = noise_level ** 2
|
20 |
+
transfer_function = 1 - noise_power / signal_power
|
21 |
+
filtered_signal = noisy_signal * transfer_function
|
22 |
+
return filtered_signal
|
23 |
+
|
24 |
+
# Function to apply IIR filter
|
25 |
+
def apply_iir_filter(data, cutoff_freq, sampling_rate, filter_type='butter'):
|
26 |
+
nyquist = 0.5 * sampling_rate
|
27 |
+
normal_cutoff = cutoff_freq / nyquist
|
28 |
+
|
29 |
+
if filter_type == 'butter':
|
30 |
+
b, a = butter(4, normal_cutoff, btype='low', analog=False)
|
31 |
+
elif filter_type == 'chebyshev':
|
32 |
+
b, a = cheby1(4, 0.5, normal_cutoff, btype='low', analog=False)
|
33 |
+
else:
|
34 |
+
raise ValueError("Invalid filter type")
|
35 |
+
|
36 |
+
filtered_data = lfilter(b, a, data)
|
37 |
+
return filtered_data
|
38 |
+
|
39 |
+
# Function to classify motor signal
|
40 |
+
def classify_motor_signal(data, sampling_rate, noise_level):
|
41 |
+
_, fft_result = apply_fft(data, sampling_rate)
|
42 |
+
num_components = 5
|
43 |
+
threshold_multiplier = 1.5
|
44 |
+
|
45 |
+
# Calculate mean amplitude of the first few frequency components
|
46 |
+
first_few_amplitudes = np.abs(fft_result[1:num_components + 1])
|
47 |
+
mean_first_few_amplitude = np.mean(first_few_amplitudes)
|
48 |
+
|
49 |
+
# Calculate mean amplitude of the entire FFT spectrum
|
50 |
+
mean_total_amplitude = np.mean(np.abs(fft_result))
|
51 |
+
|
52 |
+
# Calculate the ratio of mean_first_few_amplitude to mean_total_amplitude
|
53 |
+
ratio = mean_first_few_amplitude / mean_total_amplitude
|
54 |
+
|
55 |
+
# Set the threshold based on the ratio
|
56 |
+
threshold = threshold_multiplier
|
57 |
+
|
58 |
+
if ratio > threshold:
|
59 |
+
return "Faulty Motor: High-frequency components detected."
|
60 |
+
else:
|
61 |
+
return "Healthy Motor: No significant fault detected."
|
62 |
+
|
63 |
+
# Function to generate power spectrum
|
64 |
+
def generate_power_spectrum(signal, sampling_rate):
|
65 |
+
n = len(signal)
|
66 |
+
frequencies = np.fft.fftfreq(n, d=1/sampling_rate)
|
67 |
+
spectrum = np.abs(np.fft.fft(signal))
|
68 |
+
return frequencies, spectrum
|
69 |
+
|
70 |
+
# Function to identify peak in the power spectrum
|
71 |
+
def identify_peak(frequencies, spectrum):
|
72 |
+
peak_frequency = frequencies[np.argmax(spectrum)]
|
73 |
+
peak_amplitude = np.max(spectrum)
|
74 |
+
return peak_frequency, peak_amplitude
|
75 |
+
def calculate_mean_variance(data):
|
76 |
+
mean_value = np.mean(data)
|
77 |
+
variance_value = np.var(data)
|
78 |
+
return mean_value, variance_value
|
79 |
+
|
80 |
+
def apply_tukey_test(data, threshold=1.5):
|
81 |
+
q75, q25 = np.percentile(data, [75, 25])
|
82 |
+
iqr = q75 - q25
|
83 |
+
lower_bound = q25 - threshold * iqr
|
84 |
+
upper_bound = q75 + threshold * iqr
|
85 |
+
outliers = (data < lower_bound) | (data > upper_bound)
|
86 |
+
return outliers
|
87 |
+
|
88 |
+
# Function to classify motor based on mean, variance, and Tukey test
|
89 |
+
def classify_motor_stats(data):
|
90 |
+
mean_value, variance_value = calculate_mean_variance(data)
|
91 |
+
outliers = apply_tukey_test(data)
|
92 |
+
|
93 |
+
if np.any(outliers):
|
94 |
+
return "Faulty Motor: Outliers detected."
|
95 |
+
else:
|
96 |
+
return "Healthy Motor: No significant outliers detected."
|
97 |
+
|
98 |
+
|
99 |
+
# Streamlit app
|
100 |
+
def main():
|
101 |
+
st.title("Motor Signal Analysis App")
|
102 |
+
|
103 |
+
# Define tabs for different scenarios
|
104 |
+
tab_names = ["No Load - Healthy", "No Load - Faulty", "Full Load - Healthy", "Full Load - Faulty"]
|
105 |
+
selected_tab = st.radio("Select Scenario", tab_names)
|
106 |
+
|
107 |
+
# Set default values
|
108 |
+
default_noise_level = 0.1
|
109 |
+
default_cutoff_freq = 50
|
110 |
+
|
111 |
+
# Upload CSV file
|
112 |
+
uploaded_file = st.file_uploader(f"Upload CSV for {selected_tab}", type=["csv"])
|
113 |
+
|
114 |
+
if uploaded_file is not None:
|
115 |
+
# Load data
|
116 |
+
df = pd.read_csv(uploaded_file)
|
117 |
+
data_column = df.columns[0] # Assume data is in the first column
|
118 |
+
data = df[data_column].values
|
119 |
+
|
120 |
+
# Display original signal graph
|
121 |
+
st.subheader("Original Signal")
|
122 |
+
original_trace = go.Scatter(x=df.index, y=df[data_column], mode='lines', name='Original Signal')
|
123 |
+
st.plotly_chart([original_trace])
|
124 |
+
|
125 |
+
# Sampling rate
|
126 |
+
sampling_rate = st.number_input("Enter the sampling rate", min_value=1, value=1000)
|
127 |
+
|
128 |
+
# Apply FFT
|
129 |
+
freq, fft_result = apply_fft(data, sampling_rate)
|
130 |
+
|
131 |
+
# Plot FFT
|
132 |
+
st.subheader("FFT Analysis")
|
133 |
+
fft_trace = go.Scatter(x=np.abs(freq), y=np.abs(fft_result), mode='lines', name='FFT Result')
|
134 |
+
|
135 |
+
# Set layout parameters
|
136 |
+
layout_fft = go.Layout(
|
137 |
+
title='FFT Result Analysis',
|
138 |
+
xaxis=dict(title='Frequency (Hz)', range=[0, 200]), # Set the range up to 200 Hz
|
139 |
+
yaxis=dict(title='Amplitude',range=[0,20000]), # Remove or comment out 'autorange' setting
|
140 |
+
)
|
141 |
+
|
142 |
+
st.plotly_chart(go.Figure(data=[fft_trace], layout=layout_fft))
|
143 |
+
|
144 |
+
# Apply selected filter
|
145 |
+
apply_filter_option = st.checkbox("Apply Filter")
|
146 |
+
if apply_filter_option:
|
147 |
+
# Cutoff frequency for IIR filter
|
148 |
+
cutoff_freq = st.number_input("Enter the cutoff frequency", min_value=1, value=default_cutoff_freq)
|
149 |
+
|
150 |
+
# Filter type for IIR filter
|
151 |
+
filter_type = st.selectbox("Select Filter Type", ['butter', 'chebyshev', 'wiener'], index=0)
|
152 |
+
|
153 |
+
# Apply selected filter
|
154 |
+
if filter_type == 'butter':
|
155 |
+
filtered_data = apply_iir_filter(fft_result, cutoff_freq, sampling_rate, filter_type='butter')
|
156 |
+
elif filter_type == 'chebyshev':
|
157 |
+
filtered_data = apply_iir_filter(fft_result, cutoff_freq, sampling_rate, filter_type='chebyshev')
|
158 |
+
elif filter_type == 'wiener':
|
159 |
+
filtered_data = apply_wiener_filter(fft_result, default_noise_level)
|
160 |
+
|
161 |
+
# Generate power spectrum after applying filter
|
162 |
+
frequencies_spectrum, spectrum = generate_power_spectrum(filtered_data, sampling_rate)
|
163 |
+
|
164 |
+
# Identify peak in power spectrum
|
165 |
+
peak_frequency, peak_amplitude = identify_peak(frequencies_spectrum, spectrum)
|
166 |
+
|
167 |
+
# Plot the results using Plotly
|
168 |
+
st.subheader(f"{filter_type.capitalize()} Filtered Signal")
|
169 |
+
filtered_trace = go.Scatter(x=np.abs(freq), y=np.abs(filtered_data), mode='lines',
|
170 |
+
name=f'{filter_type.capitalize()} Filtered Signal')
|
171 |
+
layout_filtered = go.Layout(
|
172 |
+
title='Filtered signal',
|
173 |
+
xaxis=dict(title='Frequency (Hz)',range=[0,200]),
|
174 |
+
yaxis=dict(title='Amplitude',range=[0,20000]), # Remove or comment out 'autorange' setting
|
175 |
+
)
|
176 |
+
|
177 |
+
st.plotly_chart(go.Figure(data=[filtered_trace], layout=layout_filtered))
|
178 |
+
|
179 |
+
|
180 |
+
st.subheader(f"Power Spectrum after {filter_type.capitalize()} Filter")
|
181 |
+
power_spectrum_trace = go.Scatter(x=frequencies_spectrum, y=np.abs(spectrum), mode='lines',
|
182 |
+
name=f'Power Spectrum ({filter_type.capitalize()} Filter)')
|
183 |
+
|
184 |
+
# Set layout parameters for power spectrum plot
|
185 |
+
layout_power_spectrum = go.Layout(
|
186 |
+
title=f'Power Spectrum after {filter_type.capitalize()} Filter',
|
187 |
+
xaxis=dict(title='Frequency (Hz)'),
|
188 |
+
yaxis=dict(title='Amplitude'),
|
189 |
+
)
|
190 |
+
|
191 |
+
st.plotly_chart(go.Figure(data=[power_spectrum_trace], layout=layout_power_spectrum))
|
192 |
+
|
193 |
+
# Display peak frequency and amplitude
|
194 |
+
st.subheader("Peak in Power Spectrum")
|
195 |
+
st.write(f"Frequency: {peak_frequency} Hz, Amplitude: {peak_amplitude}")
|
196 |
+
|
197 |
+
motor_status_stats = classify_motor_stats(data=data)
|
198 |
+
st.info(f"Motor Status (Mean, Variance, Tukey Test): {motor_status_stats}")
|
199 |
+
|
200 |
+
# Run the app
|
201 |
+
if __name__ == "__main__":
|
202 |
+
main()
|