Search code examples
scipy

how to normalise scipy.signal.correlate output to be between -1 and 1


Does anyone know how to normalise the output of scipy's signal.correlate function so that the return array has numbers between -1 and 1. at the moment its returning numbers between -1 and 70000.


Solution

  • AFAIK scipy.signal.correlate does not have an option for auto normalize, however you can easily normalize the signal yourself:

    import numpy as np
    def normalize(tSignal):
          # copy the data if needed, omit and rename function argument if desired
          signal = np.copy(tSignal) # signal is in range [a;b]
          signal -= np.min(signal) # signal is in range to [0;b-a]
          signal /= np.max(signal) # signal is normalized to [0;1]
          signal -= 0.5 # signal is in range [-0.5;0.5]
          signal *=2 # signal is in range [-1;1]
          return signal
    

    And more general function, normalizing a vector to range [a,b]:

    import numpy as np
    def normalize(signal, a, b):
          # solving system of linear equations one can find the coefficients
          A = np.min(signal)
          B = np.max(signal)
          C = (a-b)/(A-B)
          k = (C*A - a)/C
          return (signal-k)*C