如何使用方波拟合响应图

时间:2018-04-29 05:46:47

标签: filter curve-fitting

我正在寻找适合我的响应数据的方法/方法(图片如下所示)。因此,使用f(t) = (square(2*pi*f*t)+1)来过滤我的原始数据。但是,cftool不承认这种功能。所以,请帮助我谢谢!

enter image description here

1 个答案:

答案 0 :(得分:1)

以下功能可能允许拟合数据。它是连续的,但无处不在。这些步骤往往落在右边,而OPs数据则没有。这可能需要一些额外的工作。此外,步骤必须是等距的,但似乎是这种情况。

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np

def f( x, a, b ): # test function (that would be the one to fit, actually + a shift of edge position)
    return a + b * x**3

def f_step( x, l, func, args=None ):
    y = ( x - l / 2. ) % l - l / 2.
    y = y / l * 2.
    p = np.floor( ( x-l/2.) / (l) ) + 1
    centre = p * l
    left = centre - l / 2.
    right = centre + l / 2.
    fL = func( left, *args )
    fR = func( right, *args )
    fC = func( centre, *args )
    out = fC + sharp( y , fL - fC, fR - fC , 5 )
    return out

def sharp( x, a, b , p, epsilon=1e-1 ):
    out = a * ( 1. / abs( x + 1 + epsilon )**p - ( 2 + epsilon)**( -p ) ) / ( epsilon**( -p ) - ( 2 + epsilon )**( -p ) ) 
    out += b * ( 1. /abs( x - 1 - epsilon )**p - ( 2 + epsilon)**( -p ) ) / ( epsilon**( -p ) - ( 2 + epsilon )**( -p ) ) 
    return out

l=0.57
xList = np.linspace( -1, 1.75, 500 )
yList = [ f_step( x, l, f, args=(2, -.3 ) ) for x in xList ] 

fig1 = plt.figure( 1 )
ax = fig1.add_subplot( 1, 1, 1 )
ax.plot( xList, yList )
ax.plot( xList, f(xList, 2,-.3) )
plt.show()

看起来像:

Steps with varying height