Convert MATLAB Slicing to Python using Numpy

I am having trouble converting some MATLAB code to python. I am trying to create a signal by adding a much longer signal to the shifted copies of the base signal. 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, which uses a variable to slice, a larger array did not work, so I changed it, but now I got what I think should work, but I continue to get errors about inconsistent sizes of arrays, even if when checking the sizes in the debugger 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: minimum expected entry and exit

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)

Error EDIT2

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

. (1,4410) , , 1920

+4
2

signal signal = zeros(time.size). Matlab, NumPy 1D (N,), (N,1).

+3

, 0 :

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

0

Source: https://habr.com/ru/post/1615061/


All Articles