Skip to main content

 Smooth Transitions between two functions

In python, there are several fucntions permit us to make a smooth between two functions. It include: polyfit, curve_fit. 

 We give examples of how to use polyfit and curvefit to generate smooth data. 

1. Use curve_fit



#import two signals to mix

y1 = [ 1.0000,  0.8138,  0.5838,  0.3408,  0.1145, -0.0741, -0.2172, -0.3202, -0.3966, -0.4621, -0.5281, -0.5991, -0.6735, -0.7460, -0.8119, -0.8687, -0.9161, -0.9547, -0.9835, -0.9999, -1.0000,
        -0.9816, -0.9455, -0.8967, -0.8419, -0.7885, -0.7416, -0.7024, -0.6689, -0.6369, -0.6034, -0.5673, -0.5310, -0.4991, -0.4765, -0.4664, -0.4689, -0.4802, -0.4935, -0.5014, -0.4974, -0.4780,
        -0.4440, -0.3993, -0.3499, -0.3021, -0.2601, -0.2257, -0.1980, -0.1747, -0.1539, -0.1345, -0.1170, -0.1024, -0.0920, -0.0861, -0.0836, -0.0830, -0.0825, -0.0805, -0.0760, -0.0685, -0.0580,
        -0.0447, -0.0292, -0.0126,  0.0036,  0.0183,  0.0298,  0.0378,  0.0422,  0.0437,  0.0433,  0.0423,  0.0421,  0.0439,  0.0488,  0.0571,  0.0687,  0.0827,  0.0976,  0.1121,  0.1247,  0.1343,
         0.1401,  0.1415,  0.1381,  0.1294,  0.1153,  0.0960,  0.0723,  0.0454,  0.0169, -0.0121, -0.0406, -0.0686, -0.0961, -0.1236, -0.1512, -0.1784, -0.2038, -0.2257, -0.2423, -0.2523, -0.2561,
        -0.2556, -0.2538, -0.2540, -0.2581, -0.2656, -0.2733, -0.2760, -0.2675, -0.2434, -0.2019, -0.1447, -0.0764, -0.0040,  0.0644,  0.1200,  0.1553,  0.1650,  0.1483,  0.1091,  0.0568,  0.0032,
        -0.0412, -0.0695, -0.0814, -0.0813, -0.0764, -0.0730, -0.0740, -0.0791, -0.0859, -0.0923, -0.0978, -0.1032, -0.1098, -0.1180, -0.1265, -0.1334, -0.1376, -0.1392, -0.1403, -0.1431, -0.1483,
        -0.1540, -0.1554, -0.1462, -0.1208, -0.0766, -0.0154,  0.0563,  0.1302,  0.1973,  0.2517,  0.2916,  0.3191,  0.3401,  0.3615,  0.3903,  0.4304,  0.4811,  0.5363,  0.5841,  0.6088,  0.5946,
         0.5302,  0.4136,  0.2534,  0.0687, -0.1163, -0.2782, -0.4011, -0.4799, -0.5200, -0.5341, -0.5370, -0.5404, -0.5508, -0.5689, -0.5915, -0.6134, -0.6298, -0.6371, -0.6329, -0.6162, -0.5870,
        -0.5462, -0.4963, -0.4404, -0.3822, -0.3255, -0.2733, -0.2275, -0.1890, -0.1578, -0.1338, -0.1166, -0.1062, -0.1022, -0.1042, -0.1118, -0.1239, -0.1399, -0.1590, -0.1797, -0.2006, -0.2199,
        -0.2358, -0.2473, -0.2544, -0.2583, -0.2615, -0.2663, -0.2741, -0.2840, -0.2927, -0.2954, -0.2868, -0.2629, -0.2225, -0.1675, -0.1033, -0.0366,  0.0257,  0.0784,  0.1194,  0.1498,  0.1734,
         0.1948,  0.2186,  0.2475,  0.2819,  0.3200,  0.3587,  0.3942,  0.4238,  0.4464,  0.4635,  0.4787,  0.4970,  0.5233,  0.5606,  0.6079,  0.6596,  0.7049,  0.7289,  0.7156,  0.6516,  0.5301,
         0.3542,  0.1381, -0.0948, -0.3157]
y2 = [ 0.3870,  0.1878,  0.0191, -0.1087, -0.1930, -0.2365, -0.2469, -0.2346, -0.2116, -0.1900, -0.1798, -0.1877, -0.2164, -0.2642, -0.3268, -0.3978, -0.4710, -0.5413, -0.6055, -0.6619, -0.7107,
        -0.7528, -0.7895, -0.8223, -0.8523, -0.8808, -0.9086, -0.9361, -0.9627, -0.9859, -1.0000, -0.9949, -0.9560, -0.8667, -0.7133, -0.4913, -0.2106,  0.1036,  0.4139,  0.6787,  0.8619,  0.9409,
         0.9117,  0.7882,  0.5977,  0.3736,  0.1486, -0.0510, -0.2075, -0.3127, -0.3668, -0.3765, -0.3538, -0.3131, -0.2690, -0.2343, -0.2177, -0.2233, -0.2505, -0.2957, -0.3536, -0.4189, -0.4867,
        -0.5532, -0.6150, -0.6701, -0.7174, -0.7571, -0.7902, -0.8183, -0.8431, -0.8660, -0.8881, -0.9092, -0.9269, -0.9354, -0.9235, -0.8754, -0.7740, -0.6062, -0.3706, -0.0815,  0.2311,  0.5268,
         0.7647,  0.9126,  0.9545,  0.8924,  0.7448,  0.5407,  0.3132,  0.0930, -0.0957, -0.2375, -0.3265, -0.3647, -0.3606, -0.3267, -0.2779, -0.2291, -0.1933, -0.1794, -0.1916, -0.2287, -0.2855,
        -0.3548, -0.4287, -0.5002, -0.5648, -0.6202, -0.6669, -0.7070, -0.7431, -0.7773, -0.8112, -0.8454, -0.8796, -0.9123, -0.9401, -0.9567, -0.9517, -0.9109, -0.8185, -0.6624, -0.4399, -0.1623,
         0.1442,  0.4425,  0.6924,  0.8596,  0.9238,  0.8825,  0.7506,  0.5557,  0.3309,  0.1081, -0.0873, -0.2390, -0.3402, -0.3922, -0.4024, -0.3822, -0.3450, -0.3044, -0.2717, -0.2553, -0.2597,
        -0.2854, -0.3293, -0.3866, -0.4510, -0.5166, -0.5786, -0.6336, -0.6805, -0.7191, -0.7507, -0.7770, -0.7999, -0.8212, -0.8423, -0.8636, -0.8850, -0.9043, -0.9171, -0.9147, -0.8837, -0.8077,
        -0.6718, -0.4682, -0.2029,  0.1028,  0.4144,  0.6910,  0.8948,  1.0000,  0.9984,  0.9001,  0.7295,  0.5184,  0.2990,  0.0982, -0.0655, -0.1832, -0.2542, -0.2837, -0.2811, -0.2574, -0.2245,
        -0.1934, -0.1737, -0.1722, -0.1922, -0.2331, -0.2911, -0.3600, -0.4327, -0.5032, -0.5671, -0.6223, -0.6687, -0.7075, -0.7406, -0.7702, -0.7979, -0.8247, -0.8509, -0.8760, -0.8997, -0.9209,
        -0.9371, -0.9420, -0.9245, -0.8690, -0.7594, -0.5845, -0.3447, -0.0560,  0.2510,  0.5358,  0.7579,  0.8860,  0.9055,  0.8209,  0.6533,  0.4346,  0.2000, -0.0184, -0.1972, -0.3227, -0.3920,
        -0.4105, -0.3896, -0.3442, -0.2901, -0.2417, -0.2099, -0.2012, -0.2173, -0.2554, -0.3103, -0.3752, -0.4435, -0.5095, -0.5694, -0.6211, -0.6644, -0.7007, -0.7320, -0.7603, -0.7870, -0.8123,
        -0.8358, -0.8570, -0.8754, -0.8894]

a = np.float32(y1[115:120])
b = np.float32(y2[210:225])
Re = np.arange(0,len(a)+len(b))
loc = len(a)

f1_extend = np.zeros_like(Re, dtype='float32')
f1_extend[:loc] = a

f2_extend = np.zeros_like(Re,dtype='float32')
f2_extend[loc:] = b

y = np.zeros_like(Re, dtype='float32')
y[:loc]=a
y[loc:] = b
#plt.plot(f, 'g')
plt.plot(f1_extend,'r')
plt.plot(f2_extend,'b')
plt.plot(y, 'y')



To get smooth, we use curve_fit with two fixed ends via sigma


from scipy import optimize
N = 20
sigma =np.ones(N)
samples = [0, 1, 3, -4, -2, -1]

sigma[samples]= 0.01

p = (-2, 3, -1, 1 )
p = z


def f(x, *p):
    return np.poly1d(p)(x)

p1, _ = optimize.curve_fit(f, Re, y, (0, 0, 0, 0, 0), sigma=sigma)
p2, _ = optimize.curve_fit(f, Re, y, (0, 0, 0, 0, 0))

x2 = np.linspace(0, N, 100)
y2 = np.poly1d(p)(x2)
plt.plot(Re, y, "o")
plt.plot(Re, f(Re, *p1), "r", label=u"fix three points")
plt.plot(x2, f(x2, *p2), "b", label=u"no fix")
plt.legend(loc="best")


As you see, with fixed end, we sacrificed the least squared error, but keep the function pass start and stop points with the trend rise up or down.

2. Use polyfit

z = np.polyfit(Re, y,7) # poly function with order = 7, z: parameters of the polyfunc

res =np.polyval(z, Re)
res1 = np.poly1d(z)(Re)
plt.plot(res,label=u'polyval')
plt.plot(res1, label=u'poly1d')
plt.plot(y, 'o')
plt.legend(loc='best')


Ref:

Comments

Popular posts from this blog

Serial Port Profile with CC2650 and CC2640R2 (part 2) Modified to transfer ADC values up to PC Step 1:  Include ACD.h library #include <ti/drivers/ADC.h>  Step 2: Insert period event in order to update ADC read values from the potentiometer #define SSSS_ICALL_EVT                         ICALL_MSG_EVENT_ID // Event_Id_31 #define SSSS_QUEUE_EVT                         UTIL_QUEUE_EVENT_ID // Event_Id_30 #define SSSS_PERIODIC_EVT                      Event_Id_00 //SOLUTION // Bitwise OR of all events to pend on #define SSSS_ALL_EVENTS                        (SSSS_ICALL_EVT        | \                                ...

Arduino Code for test Heart Rate 7 Click

Heart Rate 7 Click is the newest module from MikroE which uses VEM8080 photodetector has a wide range spectrum from 300nm - 1000nm. To control and acquisition data, AFE4404 from TI inc. is adopted. This chip permits control 3 LED channels, and sample heart rate default 100 SPS.  A 22-bit ADC permit collecting very small changed voltage from a PD sensor. In this example we config Arduino Mega  2650 as below: Pin 4 for RST PIN 5 CLK PIN 6 INT PIN 20 SDA PIN 21 SCL Config registers follow the default of AFE4404 datasheet Page 27 with some minor changes. 1. Config Internal Clock through  AFE_CONTROL2 register addr.: 0x23 value: 0x104218  // setup all dynamic power to save energy 2. Control LED2 current through AFE_LEDCNTRL register addr: 0x22 value: 0x000100 3. Read data using PROG_TG_EN signal through AFE_TIA_GAIN register addr: 0x21 value: 0x000125 Time to start and end of PROG_TG setup through two registers: AFE_PROG_TG_STC register (...
Serial Port Profile with CC2650 and CC2640R2 This test based on code from TI support under link: For CC2650: https://github.com/ti-simplelink/ble_examples/blob/ble_examples-2.2/docs/spp_ble.md For CC2640R2: https://github.com/ti-simplelink/ble_examples/blob/simplelink_cc2640r2_sdk-2.20/examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_client/readme.md In this experiment, I use CC2640R2 as Server (Peripheral) and CC2650 as Client (Central) as the image below: Step1: import project spp_ble_client_cc2650lp (app and stack) for CC2650 Step2: modify Code of Service in file serial_port_service:               C0E0 ==> C0C0               C0E1 ==> C0C1               C0E2 ==> C0C2 If you don't change, Two Blue Tooth Board cant know to service each other. Step 3: import project simple_serial_socket_client_CC2640r2 for CC2640R2 here we using RealTerm...