-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnoise_test.py
More file actions
55 lines (40 loc) · 1.57 KB
/
Copy pathnoise_test.py
File metadata and controls
55 lines (40 loc) · 1.57 KB
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
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtWidgets, QtCore
import pandas as pd
csv_path = "mmg.csv"
csvFile = pd.read_csv(csv_path)
x= csvFile.iloc[:, 0].values
y= csvFile.iloc[:, 1].values
# User-defined SNR value in dB
snr_db = float(input("Enter desired SNR (dB): "))
# snr range from -10 to 50 dB
# Calculate signal power as the mean square of the signal
signal_power = np.mean(y ** 2)
# Calculate noise power based on desired SNR
snr_linear = 10 ** (snr_db / 10) # Convert dB to linear scale
noise_power = signal_power / snr_linear
# Generate Gaussian noise and scale it to match the desired noise power
noise = np.random.normal(0, np.sqrt(noise_power), y.shape)
# Create the noisy signal by adding noise to the y-axis values
noisy_y = y + noise
# Initialize the PyQt application
app = QtWidgets.QApplication([])
# Set up the plot window
win = pg.GraphicsLayoutWidget(show=True, title="Signal with Noise")
win.resize(800, 500)
win.setWindowTitle('Signal Plot')
# Create a plot within the window
plot = win.addPlot(title="Original Signal vs Noisy Signal")
plot.setLabel('left', "Amplitude")
plot.setLabel('bottom', "Time", units='s')
# Plot the original signal
original_pen = pg.mkPen(color=(0, 0, 255, 150), width=2) # Blue with some transparency
plot.plot(x, y, pen=original_pen, name="Original Signal")
# Plot the noisy signal
noisy_pen = pg.mkPen(color=(255, 0, 0, 100), width=2, dash=[4, 2]) # Red with more transparency
plot.plot(x, noisy_y, pen=noisy_pen, name="Noisy Signal")
# Add a legend
plot.addLegend()
if __name__ == '__main__':
app.exec()