0

I need to remove the DC part of a signal. To do this I have this code:

def removeDC(seq):
    mean_seq = np.mean(seq)
    seq_mod = np.array([])
    for sample in seq:
        seq_mod = np.append(seq_mod,sample - mean_seq)
    return seq_mod

But my data has the dimensions/shape of (31250, 5), and i want it to remove DC form every channel. This is my try to do this, but im not sure how i add the correct value to the correct channel index

def removeDC(seq):  #removing dc from signal, slik at amplituden varierer rundt 0
    for i in range(0,seq.shape[1])):

        mean_seq = np.mean(seq[:,i])
        seq_mod = np.array([]) #need seq.shape[1] dimensions, problem here
        for sample in seq[:,i]: #problem here
            seq_mod[,i] = np.append(seq_mod, sample - mean_seq)#problem here
    return seq_mod

1 Answer 1

1

Another way to frame your problem is like this: You have data in 2 dimensions. Lets call the first one 'row' and the second 'columns' (this is standard vocabulary, but in your application they are probably 'time' and 'channel'). You have 31250 rows and 5 columns. You want to compute the mean for each column, giving you a vector of 5 values. Then, for each of the 31250 rows, you want to remove the 5 mean values. Lets do this!

data = np.random.random(size=(31250,5)) # just some random values...
means = data.mean(axis=0) #compute mean along the columns 
data = data - means 

The last line works since numpy identifies your 5 columns with the length 5 of the vector means.

In your specific case, you might want to call them something like

signals  = np.random.random(size=(31250,5))
dc_offset = signals.mean(axis=0)
centered_signals = signals  - dc_offset 

PS check out the concept of broadcasting. it is key for writing manageble numpy code. See https://numpy.org/doc/stable/user/basics.broadcasting.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.