Search code examples
pythonnumpyscipyscientific-computing

Average a single period of a periodic signal with numpy


I made a measurement in university. The signal has a lot of noise, but is periodic.

My raw signal

I know know the point where the signal starts (x=36400) and what frequency (1Hz) and samplerate (48000) are. So I can "cut" single periods every 48000 points. I can produce arrays looking like this [[period1],[period2],...,[period100]], where each period contains the measured values.

I now want to average over every single period to get a less noisy signal. I know how to do this with for-loops, but is there any fast way to use numpy for this?


Solution

  • First you'll want to slice your array to get the meaningful part

    n_periods = 10  # or however man
    beginning_idx = 1000  # or whever the good data begins
    
    raw_signal = ...  # this is the data you read in
    good_signal = raw_signal[beginning_idx:beginning_idx + n_periods * 48000]
    periodic = good_signal.reshape(n_periods, 48000)
    avg_signal = periodic.mean(axis=0)