Python Forum
Translate some code form C++ (complex vector)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate some code form C++ (complex vector)
#11
A trick, if you work with two different same shaped arrays:

import numpy as np


shape = (64,)
# one dimension
# 64 elements


real_part = np.random.random(shape) # is a np.ndarray of dtype np.float64
imag_part = np.random.random(shape) # is a np.ndarray of dtype np.float64

complex_array = real_part + 1j * imag_part
After this calculation dtype of complex_array is: np.complex128
64-Bit Float for real part and 64-bit float for imaginary part.
buran likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
I wrote this :

import numpy as np
from math import pi

def dft(signal):
    N = 1000
    K = N
    k = 0
    n = 0
    wList = []

    for k in range(k,K):
        intSum = np.complex(0,0)
        for n in range(n,N):
            real_part = np.cos(((2 * np.pi) / N) * k * n)
            imag_part = np.sin(((2 * np.pi) / N) * k * n)
            wList.append(np.complex(real_part,-imag_part))
            intSum = intSum + signal * wList
    
    return intSum
        

sigK = 3
sigPhase = 0.0
x = 0
N = 1000
signal = np.array(np.complex(0,0))
for x in range(x,N):
    Sample = np.complex(np.cos((2*pi/N)*x + sigPhase), 0.0)
    signal.append(Sample)

print(dft(signal))
I want that signal will be array complex of points and argument of dft function. I think is almost all but still I can't manage with multiply two complex arrays and append it.
When I not don't change code I've an error:

Error:
intSum = intSum + signal * wList ValueError: operands could not be broadcast together with shapes (2,) (3,)
When I don't put
Sample
into
signal
array I see
Error:
line 29, in <module> signal.append(Sample) AttributeError: 'numpy.ndarray' object has no attribute 'append'
intSum should keeps signal multiply by real and imag parts (wList)

When I did
np.append(signal, Sample)
Output of it (print(np.append(signal,Sample))) (is one point instead one thousand
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create a function vector Cooketaker 4 1,010 Dec-27-2022, 08:22 PM
Last Post: Cooketaker
Photo How do you translate this in Python? pythonflea 5 2,686 Jan-24-2021, 07:58 PM
Last Post: Serafim
  How do I translate this into python code? (excel vlookup) eeps24 6 2,835 May-29-2020, 05:54 PM
Last Post: eeps24
  matrix by vector mcgrim 8 3,894 May-02-2019, 10:39 AM
Last Post: ichabod801
  Vector field cross product Eduard 2 2,581 Aug-20-2018, 02:54 PM
Last Post: Eduard

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020