I keep getting TypeError: unsupported operand type(s) for +: 'float' and 'list'
.
I am new to python programming and am trying to convert Matlab code into Python. Below is the tried python code.
import math
import cmath
import numpy as np
import random
Tp = .1e-6
Xc = 2.e3
c = 3e8
B0 = 100e6
X0 = 50
w0 = 2 * cmath.pi * B0
fc = 1e9
wc = 2*cmath.pi * fc
alpha = w0 / Tp
wcm = wc - alpha * Tp
Ts = (2 * (Xc - X0)) / c
Tf = (2 * (Xc - X0)) / c + Tp
dt = cmath.pi / (2 * alpha * Tp)
n = 2 * math.ceil((0.5 * (Tf - Ts)) / dt)
t = Ts + np.arange(0, n*dt, dt)
dw = 2*cmath.pi / (n * dt)
w = wc + dw * np.arange(-n/2,n/2)
xn = [0, 35, 36.5, -25]
fn = [1, 0.8, 1, 0.8]
ntarget = 4
# SIMULATION
s = np.zeros((1,n))
na = 8
ar = random.uniform(1,na)
ter = 2 * cmath.pi * random.uniform(1,na)
for i in np.arange(ntarget):
td = t - (2 * (Xc + xn[i]) / c)
pha = wcm * td + alpha * np.power(td, 2)
for j in np.arange(na):
pha = pha + ar[j] * math.cos((w[n/2 + 1 + [j]] - wc) * td + ter[j])
print('pha :', pha)
Error message:
Error at
pha = pha + ar[j] * math.cos((w[n/2 + 1 + [j]] - wc) * td + ter[j])
TypeError: unsupported operand type(s) for +: 'float' and 'list'
Expected output of pha
is an array/list of numbers between 1 and 286, i.e [-940.7325, -729.3720,......,6.6187*e4,6.6449*e4]
I appreciate all the help I can get. Thanks a lot.