0

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!

2
  • 3
    I don't see what the title of this question has to do with the problem at hand. Commented Jul 19, 2014 at 14:29
  • What is the loop for? Do you want overlapping or separate samples of length three? Or just the first three? The minimal fix could be signal[:3] = [0, 0, 0]. Commented Jul 19, 2014 at 15:03

2 Answers 2

2

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.

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

1 Comment

frank128791, excellent it worked thanks a lot for your help. Yes I was to store that and make changes
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.