平滑信号 - 卷积

时间:2016-06-10 12:54:36

标签: python filtering convolution

我在Python中有一个迭代模型,它使用包含衍生物的函数生成信号。随着模型迭代,信号变得嘈杂 - 我怀疑它可能是计算数值导数的问题。我试图通过应用低通滤波器来平滑这一点,用高斯内核对噪声信号进行卷积。我使用代码段:

nw = 256
std = 40
window = gaussian(nw, std, sym=True)
filtered = convolve(current, window, mode='same') / np.sum(window)

其中current是我的信号,gaussianconvolve已从scipy导入。这似乎略有改进,前2或3次迭代看起来非常平滑。然而,在此之后,尽管低通滤波器位于迭代环路内,但信号再次变得非常嘈杂。

任何人都可以建议我可能出错的地方或我如何更好地解决这个问题?感谢。

编辑:正如我所建议的,我已经包含了以下使用的代码。在5次迭代中,信号上的噪声非常明显。

import numpy as np
from scipy import special
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from scipy.signal import convolve 
from scipy.signal import gaussian

# Constants
B =   426400E-9 # tesla
R = 71723E3
Rkm = R / 1000.
Omega =  1.75e-4 #8.913E-4 # rads/s
period = (2. * np.pi / Omega) / 3600.   # Gets period in hours
Bj = 2.0 * B
mdot = 1000.
sigmapstar = 0.05


# Create rhoe array
rho0 = 5.* R 
rho1 = 100. * R 
rhoe = np.linspace(rho0, rho1, 2.E5) 


# Define flux function and z component of equatorial field strength
Fe = B * R**3 / rhoe
Bze = B * (R/rhoe)**3

def derivs(u, rhoe, p):
    """Computes the derivative"""

    wOmegaJ = u
    Bj, sigmapstar, mdot, B, R = p

    # Compute the derivative of w/omegaJ wrt rhoe (**Fe and Bjz have been subbed)
    dwOmegaJ = (((8.0*np.pi*sigmapstar*B**2 * (R**6)) / (mdot * rhoe**5)) \
    *(1.0-wOmegaJ) - (2.*wOmegaJ/rhoe))
    res = dwOmegaJ
    return res 


its = 5 # number of iterations to perform
i = 0
# Loop to iterate
while i < its:

    # Define the initial condition of rigid corotation
    wOmegaJ_0 = 1
    params = [Bj, sigmapstar, mdot, B, R]
    init = wOmegaJ_0

    # Compute numerical solution to Hill eqn
    u = odeint(derivs, init, rhoe, args=(params,))
    wOmega = u[:,0]


    # Calculate I_rho
    i_rho = 8. * np.pi * sigmapstar * Omega * Fe * ( 1. - wOmega)

    dx = rhoe[1] - rhoe[0]
    differential = np.gradient(i_rho, dx)
    jpara = 1. * differential / (4 * np.pi * rhoe * Bze )  
    jpari = 2. * B * para

    # Remove infinity and NaN values)
    jpari[~np.isfinite(jpari)] = 0.0 

    # Convolve to smooth curve 
    nw = 256
    std = 40
    window = gaussian(nw, std, sym=True)
    filtered = convolve(jpari, window, mode='same') /np.sum(window)
    jpari = filtered 

    # Pedersen conductivity as function of jpari
    sigmapstar0 = 0.05
    jstar = 0.01e-6
    jstarstar = 0.25e-6
    s1 = 0.1e6#0.1e6  # (Am^-2)^-1
    s2 = 9.9e6  # (Am^-2)^-1
    n = 8.

    # Calculate news sigmapstar. Realistic conductivity
    sigmapstarNew =  sigmapstar0 + 0.5 * (s1 + s2/(1 + (jpari/jstarstar)**n)**(1./n)) * (np.sqrt(jpari**2 + jstar**2) + jpari)
    sigmapstarNew = sigmapstarNew

    diff = np.abs(sigmapstar - sigmapstarNew) / sigmapstar  *  100
    diff = max(diff)

    sigmapstar = 0.5* sigmapstar + 0.5* sigmapstarNew  # Weighted averaging

    i += 1

    print diff

# Plot jpari
ax = plt.subplot(111)
ax.plot(rhoe/R, jpari * 1e6)
ax.axhline(0, ls=':')
ax.set_xlabel(r'$\rho_e / R_{UCD}$')
ax.set_ylabel(r'$j_{\parallel i} $ / $ \mu$ A m$^{-2}$')
ax.set_xlim([0,80])
ax.set_ylim(-0.01,0.01)
plt.locator_params(nbins=5)

plt.draw()
plt.show()

0 个答案:

没有答案