Home > database >  Using a signal as an input to a function adds noise to the signal in Python
Using a signal as an input to a function adds noise to the signal in Python

Time:12-01

I have a signal X,

t,X = genS(f,T,L)that looks like this:
plt.plot(t,X)

plotted X, very clean

Clearly it's a very clean signal with no noise. On the next line, I use this signal as input into a function. If I then plot the same signal again...

[p,d] = bopS(X,R,T,I,fs)
plt.plot(t,X)

X has noise randomly

There is nothing else done in the code between generating and using the signal, there is not even any modification of X inside bopS, I simply call it for a calculation. Any idea what is going on here?

bopS function

def bopS(s,R,T,I,fs):
    s2 = s
    s1 = s2   np.random.normal(0,0.1*max(s2),len(s2))
    d = (R T)/(I*fs)
    s1 = np.roll(s1,d)

    return s1,d


CodePudding user response:

If you could provide the details of genS & bopS, it would help. With not knowing what these functions do then no one will be able to help.

Are these functions from a library? What library? If not share the function code.

EDIT:

I believe the issue is with you creating a "shallow" copy of the list in bopS

s2 = s

https://docs.python.org/3/library/copy.html

which means that s2 is still linked to s, anything that happens to s2 will happen to s and in this example the funciton is adding noise. To resolve this issue use the following below code.

import copy # at top of code


def bopS(s,R,T,I,fs):
    s2 = copy.deepcopy(s) #changed this form s2 = s which was a shallow copy of the list meaning it was still linked.
    s1 = s2   np.random.normal(0,0.1*max(s2),len(s2))
    d = (R T)/(I*fs)
    s1 = np.roll(s1,d)

    return s1,d

let me know if it resolved your issue.

  • Related