23 lines
753 B
Python
23 lines
753 B
Python
import numpy as np
|
|
|
|
# Calculate the signal to noise ratio given a signal and a noisy approximation
|
|
def snr(signal, noisy_signal):
|
|
noise = noisy_signal - signal # Extract just the noise
|
|
|
|
P_signal = np.sum(signal**2) # Calculate the signal power
|
|
P_noise = np.sum(noise**2) # Calculate the noise power
|
|
|
|
return P_signal/P_noise
|
|
|
|
|
|
# Calculate the mean squared error given a signal and a noisy approximation
|
|
def mse(signal, noisy_signal):
|
|
squared_difference = (signal - noisy_signal)**2
|
|
return np.sum(squared_difference)/signal.size
|
|
|
|
|
|
# Calculate the peak signal to noise ratio for a signal and a noisy approximation
|
|
def psnr(signal, noisy_signal):
|
|
max_i = 255
|
|
return max_i**2 / mse(signal, noisy_signal)
|