I guess something is wrong with this I believe. I have an array of data set I'm trying to perform some analysis on. This is what I want to do. Say for example the following is the array
signal=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...]
, I want to take the data points 0:3
stored somewhere, I need them and also replace those 0:3
with zeros. This is how I did it but the final result comes out right but the stored 0:3
data points stored also come out to be zeros. Can anyone help me out here. I thought it was something simple to do but I have been battling with this for the past couple of days. Thanks in advance!
here is my code:
n = len(signal)
for i in range(n):
first_3points = signal[0:3]
signal[0:3] = 0
trancated_signal = signal
I will be very glad to see where I went wrong!
It looks like your application is better served with numpy, which is well-developed to work with arrays representing signal samples. You may already be using numpy, since if signal
is a list, the assignment signal[0:3] = 0
raises a TypeError
. Here's how I'd do it using numpy:
import numpy as np
N = 256
signal = np.ones(N)
first3 = signal[0:3].copy()
signal[0:3] = 0
Note that if you don't make first3
a copy of the first elements in signal
, it just becomes a view into signal
, and when you change elements in signal
, you also change first3
. If I understand your question correctly, you are trying to save the original elements from signal
in first3
before you change them.
Using ordinary lists instead of numpy this is quite simple:
signal = [1] * 20
first_3_points = signal[:3]
signal[:3] = [0] * 3
The loop in your original code appears to be unnecessary.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.