-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_file.py
120 lines (95 loc) · 3.56 KB
/
python_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
"""ae23b102_assignment-6.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1M_a3INTrC2OGL5jzGx3SSSPggVawqcqH
"""
import numpy as np
import matplotlib.pyplot as plt
Noise ="/content/drive/MyDrive/Noise.txt"
signal = "/content/drive/MyDrive/Signal.txt"
Noise =input("Enter the path of the noise file: ")
signal = input("Enter the path of the signal file: ")
# Function to read data from a file and return x and y coordinates
def read_signal(file_path):
x_coords = []
y_coords = []
with open(file_path, 'r') as file:
for line in file:
x, y = map(float, line.split())
x_coords.append(x)
y_coords.append(y)
return np.array(x_coords), np.array(y_coords)
# Reading signal.txt and noise.txt data
x_signal, y_signal = read_signal(signal)
x_noise, y_noise = read_signal(Noise)
# FFT of the noisy and original signals
y_fft_signal = np.fft.fft(y_signal)
y_fft_noise = np.fft.fft(y_noise)
# Frequencies corresponding to the FFT values
frequencies_signal = np.fft.fftfreq(len(y_signal), (x_signal[1] - x_signal[0]))
frequencies_noise = np.fft.fftfreq(len(y_noise), (x_noise[1] - x_noise[0]))
# Plot the original signal and noisy signal in the time domain
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(x_signal, y_signal, label='Original Signal (signal.txt)')
plt.title('Original Signal in Time Domain')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(x_noise, y_noise, label='Noisy Signal (noise.txt)', color='orange')
plt.title('Noisy Signal in Time Domain')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.savefig('time_domain_signals.png') # Save the time domain signals plot
plt.show()
# Plot the frequency spectrum (magnitude of FFT) for signal.txt and noise.txt
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(frequencies_signal, np.abs(y_fft_signal), label='Original Signal FFT')
plt.title('Frequency Domain of Original Signal (signal.txt)')
plt.xlabel('Frequency')
plt.ylabel('Amplitude')
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(frequencies_noise, np.abs(y_fft_noise), label='Noisy Signal FFT', color='orange')
plt.title('Frequency Domain of Noisy Signal (noise.txt)')
plt.xlabel('Frequency')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.savefig('frequency_domain_signals.png')
plt.show()
#applying threshold
amplitude_threshold = 250 # keeping amplitude as 250
y_fft_noise_filtered = np.where(np.abs(y_fft_noise) > amplitude_threshold, y_fft_noise, 0)
# Plot the filtered frequency spectrum of noise.txt
plt.figure(figsize=(12, 6))
plt.plot(frequencies_noise, np.abs(y_fft_noise_filtered), label='Filtered Noisy Signal FFT', color='green')
plt.title('Filtered Frequency Domain of Noisy Signal (After Amplitude Threshold)')
plt.xlabel('Frequency')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.savefig('filtered_frequency_domain_signal.png')
plt.show()
y_noise_filtered = np.fft.ifft(y_fft_noise_filtered)
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(x_signal, y_signal, label='Original Signal (signal.txt)')
plt.title('Original Signal in Time Domain')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)
plt.subplot(2, 1, 2)
plt.plot(x_noise, np.real(y_noise_filtered), label='Filtered Noisy Signal', color='green')
plt.title('Filtered Noisy Signal in Time Domain (After Amplitude Threshold)')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)
plt.tight_layout()
plt.savefig('time_domain_comparison.png')
plt.show()