我想在Python中模拟的100个bin信号中添加一些随机噪声 - 使其更加真实。
在基本层面上,我的第一个想法是逐个bin,只是在某个范围之间生成一个随机数,然后从信号中加上或减去它。
我希望(因为这是python)可能有更智能的方法通过numpy或其他东西来做到这一点。 (我认为理想情况下,从高斯分布中提取并添加到每个bin中的数字也会更好。)
提前感谢您的回复。
我正处于规划代码的阶段,因此我没有任何要展示的内容。我只是觉得可能有更复杂的方法来产生噪音。
就输出而言,如果我有10个具有以下值的箱子:
Bin 1:1 彬2:4 彬3:9 彬4:16 彬5:25 彬6:25 彬7:16 彬8:9 彬9:4 彬10:1
我只是想知道是否有一个预先定义的函数可以添加噪音给我类似的东西:
Bin 1:1.13 Bin 2:4.21 彬3:8.79 彬4:16.08 彬5:24.97 彬6:25.14 彬7:16.22 彬8:8.90 彬9:4.02 宾10:0.91
如果没有,我将逐个bin地添加从高斯分布中选择的数字到每个。
谢谢。
这实际上是我正在模拟的射电望远镜发出的信号。我希望能够最终选择模拟的信噪比。
答案 0 :(得分:82)
您可以生成噪声数组,并将其添加到信号中
import numpy as np
noise = np.random.normal(0,1,100)
# 0 is the mean of the normal distribution you are choosing from
# 1 is the standard deviation of the normal distribution
# 100 is the number of elements you get in array noise
答案 1 :(得分:46)
......对于那些像我一样的人,他们的学习曲线非常早,
import numpy as np
pure = np.linspace(-1, 1, 100)
noise = np.random.normal(0, 1, pure.shape)
signal = pure + noise
答案 2 :(得分:12)
对于那些试图在SNR和numpy生成的普通随机变量之间建立联系的人:
[1] ,需要牢记P是平均幂。
或以dB为单位:
[2]
在这种情况下,我们已经有一个信号,并且我们希望产生噪声以提供所需的SNR。
虽然噪声可能来自不同的flavors,但取决于您的建模对象,Additive White Gaussian Noise (AWGN)是一个好的开始(尤其是对于此射电望远镜示例)。如先前的回答所述,要对AWGN建模,您需要在原始信号中添加零均值高斯随机变量。该随机变量的方差将影响平均值噪声功率。
对于高斯随机变量X,平均功率(也称为第二个moment)为
[3]
因此对于白噪声,平均功率等于方差。
在python中建模时,您可以
1.根据所需的SNR和一组现有测量值计算方差,如果您希望测量结果具有相当一致的幅度值,则此方法将起作用。
2.或者,您可以将噪声功率设置为已知水平,以匹配接收器噪声。可以通过将望远镜对准自由空间并计算平均功率来测量接收器噪声。
无论哪种方式,重要的是要确保在信号中增加噪声,并在线性空间而不是dB单位中求平均值。
这里有一些代码可以生成信号并绘制电压,功率(瓦)和功率(dB):
# Signal Generation
# matplotlib inline
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()
下面是根据所需SNR添加AWGN的示例:
# Adding noise using target SNR
# Set a target SNR
target_snr_db = 20
# Calculate signal power and convert to dB
sig_avg_watts = np.mean(x_watts)
sig_avg_db = 10 * np.log10(sig_avg_watts)
# Calculate noise according to [2] then convert to watts
noise_avg_db = sig_avg_db - target_snr_db
noise_avg_watts = 10 ** (noise_avg_db / 10)
# Generate an sample of white noise
mean_noise = 0
noise_volts = np.random.normal(mean_noise, np.sqrt(noise_avg_watts), len(x_watts))
# Noise up the original signal
y_volts = x_volts + noise_volts
# Plot signal with noise
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()
# Plot in dB
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的示例:
# Adding noise using a target noise power
# Set a target channel noise power to something very noisy
target_noise_db = 10
# Convert to linear Watt units
target_noise_watts = 10 ** (target_noise_db / 10)
# Generate noise samples
mean_noise = 0
noise_volts = np.random.normal(mean_noise, np.sqrt(target_noise_watts), len(x_watts))
# Noise up the original signal (again) and plot
y_volts = x_volts + noise_volts
# Plot signal with noise
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()
# Plot in dB
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()
答案 3 :(得分:6)
对于那些想要为在pandas数据帧中加载的多维数据集添加噪声的人,甚至是numpy ndarray,这是一个例子:
import pandas as pd
# create a sample dataset with dimension (2,2)
# in your case you need to replace this with
# clean_signal = pd.read_csv("your_data.csv")
clean_signal = pd.DataFrame([[1,2],[3,4]], columns=list('AB'), dtype=float)
print(clean_signal)
"""
print output:
A B
0 1.0 2.0
1 3.0 4.0
"""
import numpy as np
mu, sigma = 0, 0.1
# creating a noise with the same dimension as the dataset (2,2)
noise = np.random.normal(mu, sigma, [2,2])
print(noise)
"""
print output:
array([[-0.11114313, 0.25927152],
[ 0.06701506, -0.09364186]])
"""
signal = clean_signal + noise
print(signal)
"""
print output:
A B
0 0.888857 2.259272
1 3.067015 3.906358
"""
答案 4 :(得分:1)
在现实生活中,您希望模拟具有白噪声的信号。您应该在信号中添加具有高斯正态分布的随机点。如果我们谈论的设备灵敏度以单位/ SQRT(Hz)给出,那么您需要设计出与该设备的标准偏差。在这里,我提供了为您执行此操作的函数“ white_noise”,其余的代码将进行演示,并检查其是否可以正常工作。
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
"""
parameters:
rhp - spectral noise density unit/SQRT(Hz)
sr - sample rate
n - no of points
mu - mean value, optional
returns:
n points of noise signal with spectral noise density of rho
"""
def white_noise(rho, sr, n, mu=0):
sigma = rho * np.sqrt(sr/2)
noise = np.random.normal(mu, sigma, n)
return noise
rho = 1
sr = 1000
n = 1000
period = n/sr
time = np.linspace(0, period, n)
signal_pure = 100*np.sin(2*np.pi*13*time)
noise = white_noise(rho, sr, n)
signal_with_noise = signal_pure + noise
f, psd = signal.periodogram(signal_with_noise, sr)
print("Mean spectral noise density = ",np.sqrt(np.mean(psd[50:])), "arb.u/SQRT(Hz)")
plt.plot(time, signal_with_noise)
plt.plot(time, signal_pure)
plt.xlabel("time (s)")
plt.ylabel("signal (arb.u.)")
plt.show()
plt.semilogy(f[1:], np.sqrt(psd[1:]))
plt.xlabel("frequency (Hz)")
plt.ylabel("psd (arb.u./SQRT(Hz))")
#plt.axvline(13, ls="dashed", color="g")
plt.axhline(rho, ls="dashed", color="r")
plt.show()
答案 5 :(得分:0)
以上真棒答案。最近,我需要生成模拟数据,而这正是我所使用的。共享以防万一,对其他人也有帮助,
import logging
__name__ = "DataSimulator"
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
import numpy as np
import pandas as pd
def generate_simulated_data(add_anomalies:bool=True, random_state:int=42):
rnd_state = np.random.RandomState(random_state)
time = np.linspace(0, 200, num=2000)
pure = 20*np.sin(time/(2*np.pi))
# concatenate on the second axis; this will allow us to mix different data
# distribution
data = np.c_[pure]
mu = np.mean(data)
sd = np.std(data)
logger.info(f"Data shape : {data.shape}. mu: {mu} with sd: {sd}")
data_df = pd.DataFrame(data, columns=['Value'])
data_df['Index'] = data_df.index.values
# Adding gaussian jitter
jitter = 0.3*rnd_state.normal(mu, sd, size=data_df.shape[0])
data_df['with_jitter'] = data_df['Value'] + jitter
index_further_away = None
if add_anomalies:
# As per the 68-95-99.7 rule(also known as the empirical rule) mu+-2*sd
# covers 95.4% of the dataset.
# Since, anomalies are considered to be rare and typically within the
# 5-10% of the data; this filtering
# technique might work
#for us(https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule)
indexes_furhter_away = np.where(np.abs(data_df['with_jitter']) > (mu +
2*sd))[0]
logger.info(f"Number of points further away :
{len(indexes_furhter_away)}. Indexes: {indexes_furhter_away}")
# Generate a point uniformly and embed it into the dataset
random = rnd_state.uniform(0, 5, 1)
data_df.loc[indexes_furhter_away, 'with_jitter'] +=
random*data_df.loc[indexes_furhter_away, 'with_jitter']
return data_df, indexes_furhter_away
答案 6 :(得分:0)
def awgn(sinal):
regsnr=54
sigpower=sum([math.pow(abs(sinal[i]),2) for i in range(len(sinal))])
sigpower=sigpower/len(sinal)
noisepower=sigpower/(math.pow(10,regsnr/10))
noise=math.sqrt(noisepower)*(np.random.uniform(-1,1,size=len(sinal)))
return noise