I am trying to wrap my head around the syntax for scipy.optimize.minimize
. I have read the docs again and again and googled but I simply don't understand how to do this. Hope someone can point me in the right direction:
I have a signal (numpy array
) that I wish to match with a template signal (same-length numpy array) in such a way, that I determine which signal baseline adjustment and amplitude multiplication factor would result in the lowest chi-square. I think I need to do something along the lines of minimizing
(baselineadjust + amplitudefactor * templatearray - signalarray)**2
I would like to use baselineadjust and amplitudefactor to subsequently modify the signal, subtracting the modified template, i.e.
newsignal = baselineadjust + amplitudefactor * templatearray - signalarray
The purpose of this maneuver is to effectively remove the template signal from the actual signal, leaving the remaining signal/noise.
Not sure if this makes sense and happy to elaborate.
#data is a (very) long list of floats, that contains a noisy signal and a repetitive signal of a specific length but with slight variations in amplitude, partially due to the noisy signal and partially because it inherently varies a bit. I wish to remove the repetitive signal from the data.
#Prior to this, I detect signals in data:
sigwindows = []
#Gather signals
for sig in signals:
sigwindow = list(data[sig-200:sig+200])
sigwindows += [sigwindow]
#Construct an averaged signal
avgsig = np.mean(sigwindows, axis=0)
for sig in signals:
sigwindow = list(data[sig-200:sig+200])
#First, I subtract the average signal
data[sig-200:sig+200] = list(np.array(sigavg) - np.array(sigwindow))
#This removes some of the signal, but due to the inherent variation in amplitude, some of it remains.
#Instead would like to do this:
data[sig-200:sig+200] = list(np.array(sigavg) - baselineadjust - amplitudefactor * np.array(sigwindow))
#... having obtained 'baselineadjust' and 'amplitudefactor' as described above, by minimizing chi-square.