In [235]:
import math
import random

# Really slow generation of synthetic data
def reallySlowGenerateTimeSeriesData(seconds,samples_per_second):
    """Generate synthetic data"""
    
    time = []
    signal = []   
    
    # generate signal
    sample_time = 0
    for s in range(seconds):        
        for sps in range(samples_per_second):
            sample_time += 1/samples_per_second
            noise = random.random()
            scaled_noise = -1 + (noise * 2)
            sample = math.sin(2*math.pi*10*sample_time) + scaled_noise
            time.append(sample_time)
            signal.append(sample)
    
    # return time and signal
    return [time,signal]
In [236]:
%lprun -f reallySlowGenerateTimeSeriesData data = reallySlowGenerateTimeSeriesData(60*60,1000)
Timer unit: 1e-06 s

Total time: 11.8689 s
File: <ipython-input-235-1eccf348d98e>
Function: reallySlowGenerateTimeSeriesData at line 5

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     5                                           def reallySlowGenerateTimeSeriesData(seconds,samples_per_second):
     6                                               """Generate synthetic data"""
     7                                               
     8         1          2.0      2.0      0.0      time = []
     9         1          1.0      1.0      0.0      signal = []   
    10                                               
    11                                               # generate signal
    12         1          1.0      1.0      0.0      sample_time = 0
    13      3601       1591.0      0.4      0.0      for s in range(seconds):        
    14   3603600    1353082.0      0.4     11.4          for sps in range(samples_per_second):
    15   3600000    1540859.0      0.4     13.0              sample_time += 1/samples_per_second
    16   3600000    1689605.0      0.5     14.2              noise = random.random()
    17   3600000    1693564.0      0.5     14.3              scaled_noise = -1 + (noise * 2)
    18   3600000    2441391.0      0.7     20.6              sample = math.sin(2*math.pi*10*sample_time) + scaled_noise
    19   3600000    1639775.0      0.5     13.8              time.append(sample_time)
    20   3600000    1509033.0      0.4     12.7              signal.append(sample)
    21                                               
    22                                               # return time and signal
    23         1          1.0      1.0      0.0      return [time,signal]
In [237]:
fig,axes=plt.subplots()
plt.plot(data[0], data[1])
plt.xlim(0, 10)
plt.ylim(-4, 4)
plt.xlabel('time')
plt.ylabel('signal')
plt.grid(True)
plt.show()