signal generator

by: rmrasul97@gmail.com, 8 years ago

Last edited: 8 years ago

I am trying to make a signal generator that will generate 100 signals after 1 iteration.

-My series is expressed as

y = a * np.exp(-t / T1) + a * np.exp(-t / T2) + a * np.exp(-t / T3) + a * np.exp(-t / T4) + a * np.exp(-t / T5)


- The T1,T2,T3,T4,T5 values are taken from another pool of values


import numpy as np
import matplotlib.pyplot as plt

a = 10
t = np.arange(2048)

values1 = np.random.uniform(1,5)
values2 = np.random.uniform(6,10)
values3 = np.random.uniform(11,15)
values4 = np.random.uniform(16,20)
values5 = np.random.uniform(21,25)
values6 = np.random.uniform(26,30)
values7 = np.random.uniform(31,35)
values8 = np.random.uniform(36,40)
values9 = np.random.uniform(41,45)
values10 = np.random.uniform(46,50)

candidates = [values1, values2, values3, values4, values5, values6, values7, values8, values9, values10]
T1 = np.random.choice(candidates)
T2 = np.random.choice(candidates)
T3 = np.random.choice(candidates)
T4 = np.random.choice(candidates)
T5 = np.random.choice(candidates)

def signal():
    count = 0
    for epoch in range(100):
        y = a * np.exp(-t / T1) + a * np.exp(-t / T2) + a * np.exp(-t / T3) + a * np.exp(-t / T4) + a * np.exp(-t / T5)
        count += 1
    return y


print(T1, T2, T3, T4, T5)
plt.plot(t,signal())



But this way I can only get 1 signal at a time. Can I generate 100 signals simultaneously?



You must be logged in to post. Please login or register an account.



The reason your code isn't working is you're redefining y every time. There are many ways to do this, here's one:

def signal():
    return a * np.exp(-t / T1) + a * np.exp(-t / T2) + a * np.exp(-t / T3) + a * np.exp(-t / T4) + a * np.exp(-t / T5)

signals = [signal() for i in range(100)]
print(signals)


-Harrison 8 years ago

You must be logged in to post. Please login or register an account.


Actually I want to generate 10 sets of the signal sequence. But for every set, I want the (T1-T5) to be chosen again. I have tried this-

nsets= 10

def signal():
    y = a * np.exp(-t / T1) + a * np.exp(-t / T2) + a * np.exp(-t / T3) + a * np.exp(-t / T4) + a * np.exp(-t / T5)
    return y

for i in range(nsets):
    signalset = []
    signalset.append(signal())
    print(signal())



After printing signal ()

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]

[  5.00000000e+01   4.29790448e+01   3.86182487e+01 ...,   1.08049681e-19
   1.05647407e-19   1.03298543e-19]


The above script gives me 10 sets of my desired signal but they are identical because the T1-T5 variables are the same for all 10 cases.

How could I make the function pick a new set of T1-T5 values , every time it creates a new signal?


-rmrasul97@gmail.com 8 years ago

You must be logged in to post. Please login or register an account.


If you want them to be random every time, you need the definitions of all those variables (the random parts) to happen in the function itself. If you define it as a global outside the function, the random choice will only happen once. Define it in the function, and it will happen every time the function is called and will be more unique.

-Harrison 8 years ago

You must be logged in to post. Please login or register an account.


ahh got it working. Thank you so much :)

-rmrasul97@gmail.com 8 years ago

You must be logged in to post. Please login or register an account.