如何在python中将噪声存储为傅里叶变换的变量

2024-10-16 17:19:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python执行傅里叶变换。我想知道,如何将从Fourier transformation获得的noise存储为variable

Python代码:

import numpy as np

# Create a simple signal with two frequencies
dt = 0.001
t = np.arange(0,1,dt)
f = np.sin(2*np.pi*50*t) + np.sin(2*np.pi*120*t) # Sum of 2 frequencies
f_clean = f
noise = 2.5*np.random.randn(len(t))
f = f + noise              # Add some noise

## Compute the Fast Fourier Transform (FFT)

n = len(t)
fhat = np.fft.fft(f,n)                     # Compute the FFT
PSD = fhat * np.conj(fhat) / n             # Power spectrum (power per freq)
freq = (1/(dt*n)) * np.arange(n)           # Create x-axis of frequencies in Hz
L = np.arange(1,np.floor(n/2),dtype='int') # Only plot the first half of freqs

## Use the PSD to filter out noise
indices = PSD > 100       # Find all freqs with large power
PSDclean = PSD * indices  # Zero out all others
fhat = indices * fhat     # Zero out small Fourier coeffs. in Y
ffilt = np.fft.ifft(fhat) # Inverse FFT for filtered time signal

我尝试使用以下代码行来存储噪波值,但不确定执行此任务的语法或方法是否正确

## To store the noise using the PSD 

low_indices = PSD < 100   # Find all freqs with small power
PSDnoise = PSD * low_indices  # zero out larger freqs
fhat_noise = low_indices * fhat

有没有更好的方法来存储这些值


Tags: ofthefftwithnpdtoutfourier
1条回答
网友
1楼 · 发布于 2024-10-16 17:19:23

如果你这样做

PSDnoise = PSD * low_indices

low_indices中有一个False的地方将有0个值

如果只想将PSD的值保持在低于阈值的索引上,可以使用数组索引,如下所示:

PSDnoise = PSD[low_indices]

相关问题 更多 >