This is the product of BALIGE ACADEMY TEAM: Vivian Siahaan and Rismon Hasiholan Sianipar.
SEMANGAT BELAJAR dan HORAS!!!
BALIGE CITY, NORTH SUMATERA
START FROM SCRATCH DIGITAL SIGNAL PROCESSING WITH TKINTER
SUPPORT OUR CHANNEL BY SUBSCRIBING TO IT:
SOURCE CODE:
#denoising_utils.py import matplotlib.pyplot as plt import numpy as np from scipy.signal import savgol_filter import cvxpy as cp from scipy.ndimage import gaussian_filter1d class Denoising_Utils: def __init__(self): pass # Define a function for moving average smoothing def moving_average(self, data, window_size): smoothed_data = np.convolve(data, np.ones(window_size)/window_size, mode='same') return smoothed_data def plot_ma_denoising(self, t, signal_in, title, window_size, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.moving_average(signal_in, window_size) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Moving Average Denoising" + " with window size = " + str(window_size)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def exponential_moving_average(self, data, alpha): ema = [data[0]] for i in range(1, len(data)): ema.append(alpha * data[i] + (1 - alpha) * ema[i-1]) return ema def plot_ema_denoising(self, t, signal_in, title, alpha, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.exponential_moving_average(signal_in, alpha) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Exponential Moving Average Denoising" + " with alpha = " + str(alpha)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def median_filter(self, signal, window_size): filtered_signal = np.zeros_like(signal) padding = window_size // 2 for i in range(len(signal)): window = sorted(signal[max(0, i - padding):min(len(signal), i + padding + 1)]) filtered_signal[i] = window[len(window) // 2] return filtered_signal def plot_median_denoising(self, t, signal_in, title, window_size, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.median_filter(signal_in, window_size) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Median Denoising" + " with window size = " + str(window_size)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def apply_savgol_filter(self, signal, window_size, order): smoothed_signal = savgol_filter(signal, window_size, order) return smoothed_signal def plot_savgol_denoising(self, t, signal_in, title, window_size, order, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_signal = self.apply_savgol_filter(signal_in, window_size, order) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_signal, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Savitzky-Golay Denoising" + " with window size = " + str(window_size) + " and Polynomial Order = " + str(order)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def wiener_filter_noisy(self, noisy_signal, noise_variance): # Compute the power spectral density (PSD) of the noisy signal noisy_psd = np.abs(np.fft.fft(noisy_signal))**2 # Estimate the PSD of the original signal using the Wiener deconvolution formula estimated_original_psd = noisy_psd - noise_variance # Apply the Wiener filter transfer function wiener_transfer_function = estimated_original_psd / (estimated_original_psd + noise_variance) # Apply the Wiener filter in the frequency domain filtered_signal = np.fft.ifft(wiener_transfer_function * np.fft.fft(noisy_signal)) return np.real(filtered_signal) def plot_wiener_denoising(self, t, signal_in, title, variance, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.wiener_filter_noisy(signal_in, variance) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Wiener Denoising" + " with variance = " + str(variance)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def total_variation_denoising(self, noisy_signal, lambda_param): # Define variables x = cp.Variable(len(noisy_signal)) # Define objective function objective = cp.Minimize(0.5 * cp.norm(x - noisy_signal, 'fro')**2 + lambda_param * cp.tv(x)) # Define problem and solve problem = cp.Problem(objective) problem.solve() return x.value def plot_tv_denoising(self, t, signal_in, title, lambda_param, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.total_variation_denoising(signal_in, lambda_param) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Total Variation Denoising" + " with lambda = " + str(lambda_param)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def nlm_1d_denoising(self, noisy_signal, sigma, patch_size, h): denoised_signal = np.zeros_like(noisy_signal) for i in range(len(noisy_signal)): start_idx = max(0, i - patch_size) end_idx = min(len(noisy_signal), i + patch_size + 1) patch = noisy_signal[start_idx:end_idx] weights = np.exp(-((patch - noisy_signal[i])**2) / (2 * h**2)) weights /= np.sum(weights) denoised_signal[i] = np.sum(weights * patch) denoised_signal = gaussian_filter1d(denoised_signal, sigma) return denoised_signal def plot_nlm_denoising(self, t, signal_in, title, sigma, patch_size, h, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.nlm_1d_denoising(signal_in, sigma, patch_size, h) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Non-Local Means (NLM) Denoising" + " with sigma = " + str(sigma) + " patch size = " + str(patch_size) + " filter strength = " + str(h)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw() def pca_denoising(self, signal, num_components): # Step 1: Create a data matrix with overlapping patches patch_size = num_components data_matrix = np.zeros((len(signal) - patch_size + 1, patch_size)) for i in range(len(signal) - patch_size + 1): data_matrix[i, :] = signal[i:i+patch_size] # Step 2: Calculate the mean of the data mean_vector = np.mean(data_matrix, axis=0) # Step 3: Subtract the mean from the data centered_data = data_matrix - mean_vector # Step 4: Calculate the covariance matrix covariance_matrix = np.cov(centered_data, rowvar=False) # Step 5: Compute eigenvectors and eigenvalues eigenvalues, eigenvectors = np.linalg.eig(covariance_matrix) # Step 6: Sort eigenvectors by eigenvalues in descending order sorted_indices = np.argsort(eigenvalues)[::-1] sorted_eigenvectors = eigenvectors[:, sorted_indices] # Step 7: Select the top 'num_components' eigenvectors selected_eigenvectors = sorted_eigenvectors[:, :num_components] # Step 8: Project the data onto the selected eigenvectors pca_result = np.dot(centered_data, selected_eigenvectors) # Step 9: Reconstruct denoised signal denoised_data = np.dot(pca_result, selected_eigenvectors.T) + mean_vector # Step 10: Extract denoised signal from overlapping patches denoised_signal = np.zeros_like(signal) for i in range(len(signal) - patch_size + 1): denoised_signal[i:i+patch_size] += denoised_data[i, :] # Step 11: Normalize denoised signal denoised_signal /= np.max(np.abs(denoised_signal)) return denoised_signal def plot_pca_denoising(self, t, signal_in, title, num_components, figure, canvas): figure.clear() ax1 = figure.add_subplot(2,1,1) ax1.plot(t, signal_in, linewidth=2.0, color='red') ax1.set_ylabel('Amplitude', color='blue') ax1.set_xlabel('Time (second)', color='blue') ax1.set_title(title) ax1.set_facecolor('#F0F0F0') ax1.grid(True) smoothed_data = self.pca_denoising(signal_in, num_components) ax2 = figure.add_subplot(2,1,2) ax2.plot(t, smoothed_data, linewidth=2.0, color='blue') ax2.set_xlabel('Time (second)', color='blue') ax2.set_title("Principal Component Analysis (PCA)" + " with number of components = " + str(num_components)) ax2.set_facecolor('#F0F0F0') ax2.grid(True) figure.tight_layout() canvas.draw()
#form_ma_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_MA_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="WINDOW SIZE") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_win_size = tk.Entry(master, width=20, bg="lightblue") self.entry_win_size.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_win_size.insert(0, "20") self.entry_win_size.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=8, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, win_size): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: window_size = int(win_size) except ValueError: window_size = 20 self.entry_win_size.delete(0, tk.END) self.entry_win_size.insert(0, "20") return fsampling, periode, noise_amp, window_size def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() size = self.entry_win_size.get() Fs, T, noise_amp, win_size = self.read_and_check_input(fsampling, periode, amp, size) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ma_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_MA_Denoising(window) window.mainloop() #form_ema_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_EMA_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="ALPHA") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_alpha = tk.Entry(master, width=20, bg="lightblue") self.entry_alpha.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_alpha.insert(0, "0.2") self.entry_alpha.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=8, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, alpha): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: alpha_val = float(alpha) except ValueError: alpha_val = 0.2 self.entry_alpha.delete(0, tk.END) self.entry_alpha.insert(0, "0.2") return fsampling, periode, noise_amp, alpha_val def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() alpha = self.entry_alpha.get() Fs, T, noise_amp, alpha_val = self.read_and_check_input(fsampling, periode, amp, alpha) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_ema_denoising(self.t, self.y, self.chosen_signal, alpha_val, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_EMA_Denoising(window) window.mainloop() #form_median_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_Median_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="WINDOW SIZE") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_win_size = tk.Entry(master, width=20, bg="lightblue") self.entry_win_size.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_win_size.insert(0, "20") self.entry_win_size.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=8, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, win_size): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: window_size = int(win_size) except ValueError: window_size = 20 self.entry_win_size.delete(0, tk.END) self.entry_win_size.insert(0, "20") return fsampling, periode, noise_amp, window_size def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() size = self.entry_win_size.get() Fs, T, noise_amp, win_size = self.read_and_check_input(fsampling, periode, amp, size) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_median_denoising(self.t, self.y, self.chosen_signal, win_size, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_Median_Denoising(window) window.mainloop() #form_sagvol_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_Sagvol_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="WINDOW SIZE") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="POLYNOMIAL ORDER") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=10, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_win_size = tk.Entry(master, width=20, bg="lightblue") self.entry_win_size.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_win_size.insert(0, "20") self.entry_win_size.bind('<Return>', self.choose_list_widget) self.entry_poly_order = tk.Entry(master, width=20, bg="lightblue") self.entry_poly_order.grid(row=9, column=0, padx=5, pady=0.1, sticky="nw") self.entry_poly_order.insert(0, "3") self.entry_poly_order.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=11, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, win_size, order): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: window_size = int(win_size) except ValueError: window_size = 20 self.entry_win_size.delete(0, tk.END) self.entry_win_size.insert(0, "20") try: poly_order = int(order) except ValueError: poly_order = 20 self.entry_poly_order.delete(0, tk.END) self.entry_poly_order.insert(0, "20") return fsampling, periode, noise_amp, window_size, poly_order def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() size = self.entry_win_size.get() order = self.entry_poly_order.get() Fs, T, noise_amp, win_size, poly_order = self.read_and_check_input(fsampling, periode, amp, size, order) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_savgol_denoising(self.t, self.y, self.chosen_signal, win_size, poly_order, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_Sagvol_Denoising(window) window.mainloop() #form_wiener_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_Wiener_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="VARIANCE") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_variance = tk.Entry(master, width=20, bg="lightblue") self.entry_variance.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_variance.insert(0, "0.1") self.entry_variance.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=8, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, var): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: variance = float(var) except ValueError: variance = 20 self.entry_variance.delete(0, tk.END) self.entry_variance.insert(0, "0.1") return fsampling, periode, noise_amp, variance def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() var = self.entry_variance.get() Fs, T, noise_amp, variance = self.read_and_check_input(fsampling, periode, amp, var) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_wiener_denoising(self.t, self.y, self.chosen_signal, variance, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_Wiener_Denoising(window) window.mainloop() #form_tv_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_TV_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="VARIANCE") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_lambda = tk.Entry(master, width=20, bg="lightblue") self.entry_lambda.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_lambda.insert(0, "0.1") self.entry_lambda.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=8, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, lamb): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: lambda_val = float(lamb) except ValueError: lambda_val = 20 self.entry_lambda.delete(0, tk.END) self.entry_lambda.insert(0, "0.1") return fsampling, periode, noise_amp, lambda_val def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() lamb = self.entry_lambda.get() Fs, T, noise_amp, lambda_val = self.read_and_check_input(fsampling, periode, amp, lamb) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_tv_denoising(self.t, self.y, self.chosen_signal, lambda_val, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_TV_Denoising(window) window.mainloop() #form_nlm_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_NLM_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="SIGMA") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="PATCH SIZE") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") self.label6 = tk.Label(master, text="FILTER STRENGTH") self.label6.grid(row=10, column=0, padx=5, pady=0.1, sticky="w") self.label7 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label7.grid(row=12, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_sigma = tk.Entry(master, width=20, bg="lightblue") self.entry_sigma.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_sigma.insert(0, "2") self.entry_sigma.bind('<Return>', self.choose_list_widget) self.entry_patch = tk.Entry(master, width=20, bg="lightblue") self.entry_patch.grid(row=9, column=0, padx=5, pady=0.1, sticky="nw") self.entry_patch.insert(0, "20") self.entry_patch.bind('<Return>', self.choose_list_widget) self.entry_strength = tk.Entry(master, width=20, bg="lightblue") self.entry_strength.grid(row=11, column=0, padx=5, pady=0.1, sticky="nw") self.entry_strength.insert(0, "0.1") self.entry_strength.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=13, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, sig, patch, strength): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: sigma = float(sig) except ValueError: sigma = 2 self.entry_sigma.delete(0, tk.END) self.entry_sigma.insert(0, "2") try: size = int(patch) except ValueError: size = 20 self.entry_patch.delete(0, tk.END) self.entry_patch.insert(0, "20") try: flt_strength = float(strength) except ValueError: flt_strength = 0.1 self.entry_strength.delete(0, tk.END) self.entry_strength.insert(0, "0.1") return fsampling, periode, noise_amp, sigma, size, flt_strength def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() sig = self.entry_sigma.get() patch = self.entry_patch.get() strength = self.entry_strength.get() Fs, T, noise_amp, sigma, patch_size, h = self.read_and_check_input(fsampling, periode, amp, sig, patch, strength) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_nlm_denoising(self.t, self.y, self.chosen_signal, sigma, patch_size, h, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_NLM_Denoising(window) window.mainloop() #form_pca_denoising.py import tkinter as tk from tkinter import ttk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from denoising_utils import Denoising_Utils from filter_utils import Filter_Utils class Form_PCA_Denoising: def __init__(self, window): self.window = window width = 1520 height = 760 self.window.geometry(f"{width}x{height}") #Adds widgets self.add_canvas(self.window) self.add_labels(self.window) self.add_listbox(self.window) self.add_entries(self.window) #Binds events self.binds_event() #Creates neccesary objects self.filter_utils = Filter_Utils() self.denoising_utils = Denoising_Utils() self.chosen_signal = "Noisy Sinusoidal" def add_labels(self, master): # Create labels self.label1 = tk.Label(master, text="SAMPLING FREQUENCY") self.label1.grid(row=0, column=0, padx=5, pady=0.1, sticky="w") self.label2 = tk.Label(master, text="TIME PERIODE") self.label2.grid(row=2, column=0, padx=5, pady=0.1, sticky="nw") self.label3 = tk.Label(master, text="NOISE AMPLITUDE") self.label3.grid(row=4, column=0, padx=5, pady=0.1, sticky="nw") self.label4 = tk.Label(master, text="NUMBER OF COMPONENTS") self.label4.grid(row=6, column=0, padx=5, pady=0.1, sticky="nw") self.label5 = tk.Label(master, text="CHOOSE TEST SIGNAL") self.label5.grid(row=8, column=0, padx=5, pady=0.1, sticky="w") def add_entries(self, master): # Create entry widgets self.entry_fs = tk.Entry(master, width=20, bg="lightblue") self.entry_fs.grid(row=1, column=0, padx=5, pady=0.1, sticky="nw") self.entry_fs.insert(0, "1000") self.entry_fs.bind('<Return>', self.choose_list_widget) self.entry_periode = tk.Entry(master, width=20, bg="lightblue") self.entry_periode.grid(row=3, column=0, padx=5, pady=0.1, sticky="nw") self.entry_periode.insert(0, "1") self.entry_periode.bind('<Return>', self.choose_list_widget) self.entry_noise = tk.Entry(master, width=20, bg="lightblue") self.entry_noise.grid(row=5, column=0, padx=5, pady=0.1, sticky="nw") self.entry_noise.insert(0, "0.1") self.entry_noise.bind('<Return>', self.choose_list_widget) self.entry_number = tk.Entry(master, width=20, bg="lightblue") self.entry_number.grid(row=7, column=0, padx=5, pady=0.1, sticky="nw") self.entry_number.insert(0, "5") self.entry_number.bind('<Return>', self.choose_list_widget) def add_listbox(self, root): #Menambahkan listbox widget self.listbox = tk.Listbox(root, selectmode=tk.SINGLE, width=25) self.listbox.grid(row=8, column=0, sticky='nw', padx=1, pady=1) # Menyisipkan item ke dalam list widget items = ["Noisy Sinusoidal", "Noisy Square Wave", "Noisy Amplitude Modulation Signal", "Noisy Frequency Modulation Signal", "Noisy Phase Modulation Signal", "Noisy ASK Modulation Signal", "Noisy FSK Modulation Signal", "Noisy PSK Modulation Signal"] for item in items: self.listbox.insert(tk.END, item) self.listbox.config(height=len(items)) def add_canvas(self, master): # Create a frame for canvas1 with a border frame1 = ttk.Frame(master, borderwidth=3, relief="groove") frame1.grid(row=0, column=1, columnspan=1, rowspan=25, padx=5, pady=5, sticky="n") # Adds canvas1 widget to frame1 self.figure1 = Figure(figsize=(13.4, 7.4), dpi=100) self.figure1.patch.set_facecolor('#F0F0F0') self.canvas1 = FigureCanvasTkAgg(self.figure1, master=frame1) self.canvas1.get_tk_widget().pack(fill=tk.BOTH, expand=True) def read_and_check_input(self, fs, T, amp, numb): try: fsampling = float(fs) except ValueError: fsampling = 1000 self.entry_fs.delete(0, tk.END) self.entry_fs.insert(0, "1000") try: periode = float(T) except ValueError: periode = 1 self.entry_periode.delete(0, tk.END) self.entry_periode.insert(0, "1") try: noise_amp = float(amp) except ValueError: noise_amp = 0.1 self.entry_noise.delete(0, tk.END) self.entry_noise.insert(0, "0.1") try: number = int(numb) except ValueError: number = 5 self.entry_number.delete(0, tk.END) self.entry_number.insert(0, "5") return fsampling, periode, noise_amp, number def binds_event(self): #Binds listbox to choose_list_widget() function self.listbox.bind("<<ListboxSelect>>", self.choose_list_widget) def choose_list_widget(self, event): self.chosen_signal = self.listbox.get(self.listbox.curselection()) #Reads and checks params fsampling = self.entry_fs.get() periode = self.entry_periode.get() amp = self.entry_noise.get() numb = self.entry_number.get() Fs, T, noise_amp, number = self.read_and_check_input(fsampling, periode, amp, numb) if self.chosen_signal == "Noisy Sinusoidal": freq_sinus = 10 self.t, self.y = self.filter_utils.generate_noisy_sinusoidal(T, freq_sinus, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Square Wave": self.t, self.y = self.filter_utils.generate_noisy_square(T, Fs, 20, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Amplitude Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_amp_modulation(T, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Frequency Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_freq_modulation(T, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy Phase Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_phase_modulation(T, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy ASK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_ask_modulation(T, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy FSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_fsk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if self.chosen_signal == "Noisy PSK Modulation Signal": self.t, self.y = self.filter_utils.generate_noisy_psk_modulation(T, Fs, noise_amp) self.denoising_utils.plot_pca_denoising(self.t, self.y, self.chosen_signal, number, self.figure1, self.canvas1) if __name__ == "__main__": window = tk.Tk() Form_PCA_Denoising(window) window.mainloop()
DOWNLOAD FULL SOURCE CODE VERSION 20.0
No comments:
Post a Comment