สำหรับผู้ที่พยายามเชื่อมต่อระหว่างSNRกับตัวแปรสุ่มปกติที่สร้างโดย numpy:
[1]
ซึ่งมันเป็นสิ่งสำคัญที่จะเก็บไว้ในใจว่า P มีค่าเฉลี่ยการใช้พลังงาน
หรือเป็น dB:
[2]
ในกรณีนี้เรามีสัญญาณอยู่แล้วและต้องการสร้างสัญญาณรบกวนเพื่อให้ได้ SNR ที่ต้องการ
ในขณะที่เสียงสามารถมาในที่แตกต่างกันรสชาติขึ้นอยู่กับสิ่งที่คุณสร้างแบบจำลองการเริ่มต้นที่ดี (โดยเฉพาะอย่างยิ่งเช่นกล้องโทรทรรศน์วิทยุนี้) เป็นสารเติมแต่งสีขาว Gaussian เสียงรบกวน (AWGN) ตามที่ระบุไว้ในคำตอบก่อนหน้านี้ในการสร้างแบบจำลอง AWGN คุณต้องเพิ่มตัวแปรสุ่มแบบเกาส์ที่มีค่าเฉลี่ยเป็นศูนย์ให้กับสัญญาณดั้งเดิมของคุณ ความแปรปรวนของตัวแปรสุ่มนั้นจะส่งผลต่อพลังเสียงเฉลี่ย
สำหรับตัวแปรสุ่ม Gaussian X กำลังเฉลี่ย
หรือที่เรียกว่าช่วงเวลาที่สองคือ
[3] 
ดังนั้นสำหรับเสียงสีขาว, และพลังงานเฉลี่ยแล้วเท่ากับแปรปรวน

เมื่อสร้างแบบจำลองนี้ใน python คุณสามารถ
1. คำนวณความแปรปรวนตาม SNR ที่ต้องการและชุดของการวัดที่มีอยู่ซึ่งจะได้ผลหากคุณคาดว่าการวัดของคุณจะมีค่าแอมพลิจูดที่ค่อนข้างสม่ำเสมอ
2. หรือคุณสามารถตั้งค่าพลังเสียงให้อยู่ในระดับที่ทราบเพื่อให้ตรงกับบางอย่างเช่นสัญญาณรบกวนของตัวรับสัญญาณ สัญญาณรบกวนของเครื่องรับสามารถวัดได้โดยชี้กล้องโทรทรรศน์ไปยังพื้นที่ว่างและคำนวณกำลังเฉลี่ย
ไม่ว่าจะด้วยวิธีใดสิ่งสำคัญคือต้องแน่ใจว่าคุณเพิ่มสัญญาณรบกวนให้กับสัญญาณของคุณและใช้ค่าเฉลี่ยในพื้นที่เชิงเส้นไม่ใช่หน่วย dB
นี่คือรหัสบางส่วนสำหรับสร้างสัญญาณและพล็อตแรงดันไฟฟ้ากำลังเป็นวัตต์และกำลังไฟเป็น dB:
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(1, 100, 1000)
x_volts = 10*np.sin(t/(2*np.pi))
plt.subplot(3,1,1)
plt.plot(t, x_volts)
plt.title('Signal')
plt.ylabel('Voltage (V)')
plt.xlabel('Time (s)')
plt.show()
x_watts = x_volts ** 2
plt.subplot(3,1,2)
plt.plot(t, x_watts)
plt.title('Signal Power')
plt.ylabel('Power (W)')
plt.xlabel('Time (s)')
plt.show()
x_db = 10 * np.log10(x_watts)
plt.subplot(3,1,3)
plt.plot(t, x_db)
plt.title('Signal Power in dB')
plt.ylabel('Power (dB)')
plt.xlabel('Time (s)')
plt.show()

นี่คือตัวอย่างสำหรับการเพิ่ม AWGN ตาม SNR ที่ต้องการ:
target_snr_db = 20
sig_avg_watts = np.mean(x_watts)
sig_avg_db = 10 * np.log10(sig_avg_watts)
noise_avg_db = sig_avg_db - target_snr_db
noise_avg_watts = 10 ** (noise_avg_db / 10)
mean_noise = 0
noise_volts = np.random.normal(mean_noise, np.sqrt(noise_avg_watts), len(x_watts))
y_volts = x_volts + noise_volts
plt.subplot(2,1,1)
plt.plot(t, y_volts)
plt.title('Signal with noise')
plt.ylabel('Voltage (V)')
plt.xlabel('Time (s)')
plt.show()
y_watts = y_volts ** 2
y_db = 10 * np.log10(y_watts)
plt.subplot(2,1,2)
plt.plot(t, 10* np.log10(y_volts**2))
plt.title('Signal with noise (dB)')
plt.ylabel('Power (dB)')
plt.xlabel('Time (s)')
plt.show()

และนี่คือตัวอย่างสำหรับการเพิ่ม AWGN ตามพลังเสียงที่ทราบ:
target_noise_db = 10
target_noise_watts = 10 ** (target_noise_db / 10)
mean_noise = 0
noise_volts = np.random.normal(mean_noise, np.sqrt(target_noise_watts), len(x_watts))
y_volts = x_volts + noise_volts
plt.subplot(2,1,1)
plt.plot(t, y_volts)
plt.title('Signal with noise')
plt.ylabel('Voltage (V)')
plt.xlabel('Time (s)')
plt.show()
y_watts = y_volts ** 2
y_db = 10 * np.log10(y_watts)
plt.subplot(2,1,2)
plt.plot(t, 10* np.log10(y_volts**2))
plt.title('Signal with noise')
plt.ylabel('Power (dB)')
plt.xlabel('Time (s)')
plt.show()
