4

I am having trouble converting some MATLAB code into python. I am trying to build a signal by adding in shifted copies of base signal into a much longer one. The code that works in MATLAB is

function [time, signal] = generateRandomSignal(pulse,data,samples,Tb)
N = length(data);
time = linspace(0,N*Tb,samples*N);
signal = zeros(1,length(time));
k = 1;
for n = 1:N
        window = k:k+samples-1;
        signal(window) = signal(window) + data(n)*pulse;
        k = k + samples;
end

In python using the variable to slice the larger array wasn't working so I changed that but now I got what I think should work but I keep getting errors about inconsistent array sizes even though when I inspect the sizes in a debugger it looks like it should work.

from numpy import *
def generateRandomSignal(pulse,data,samples,Tb):
    N = data.size;
    time = linspace(0,N*Tb,samples*N);
    signal = zeros((1,time.size));
    k = 0;
    for n in range(0,N):
            signal[k:k+samples] = signal[k:k+samples].copy() + data[n]*pulse[:].copy();
            k = k + samples;
    return time, signal

What is the correct way to do this in Python?

EDIT: Minimal expected input and output

Input
  data = [1, -1, 0, 1, 1]
  pulse = [1, 1, 1]
  samples = 3. #length of pulse
  Tb = 0.1

Output
  signal = [1, 1, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1, 1, 1, 1]
  time = vector of 15 points evenly spaced from 0 to 0.3.  (Not the problem)

EDIT2 Error

ValueError: operands could not be broadcast together with shapes (1920,) (1,4410)

That is the actual error produced. (1,4410) is the correct shape for the pulse array but I have no idea where the 1920 is coming from or what the empty comma means

8
  • 1
    Can you put a minimalistic example input and expected output? Commented Nov 9, 2015 at 15:21
  • Added a small sample input and output to hopefully explain my intention better Commented Nov 9, 2015 at 16:10
  • 1
    A bit confused with the addition part of the question. pulse has 4410 elements, but the number of samples is 1920. I'm not sure how MATLAB wasn't throwing an error because the way the two vectors are being added are inconsistent in dimensions... unless I'm not reading the code properly. Commented Nov 9, 2015 at 18:04
  • 1
    Use .shape rather than .size. Shape has information about each dimension, size is just the total number of items. Commented Nov 9, 2015 at 18:14
  • 1
    @rayryeng I still think that the code can't work for samples != len(pulse)... Unless I'm not reading it properly neither.. Commented Nov 10, 2015 at 9:52

2 Answers 2

3

Change your definition of signal to signal = zeros(time.size). Unlike Matlab, NumPy's 1D arrays have shape (N,), not (N,1).

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

1 Comment

After looking into the shapes of all my variables i found data was a (1,1920) and doing data[n] gave an array for all 1920 entries not each item one by one. After changing it to data[0,n] the code works as expected. Thanks for the tip
0

I can't see why you should have 0 index in signal:

signal[0,k:k+samples] = signal[0,k:k+samples].copy() + data[n]*pulse[:].copy();

1 Comment

That was left over from a test when I was trying to force the dimensions to match. However, removing it doesn't make a difference

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.