#๐Ÿ”’ Please help me with the Fourier transform

45 messages ยท Page 1 of 1 (latest)

merry kelp
#

I don't think I need to expand much, I am failing spectacularly. I've tried so many different ways ๐Ÿ˜ฆ . Here's my code: ```py
import numpy as np
from scipy.signal import find_peaks

class DFT:
def init(self, x: list):
self.x: list = x
self.T: int = len(x)
self.a0: float | int = sum(x) / self.T

def calc(self, freqs: int, samplePoints: int):
    ret = []
    for freq in range(freqs):
        temp = 0
        for i in range(self.T):
            temp += self.x[i] * np.sin(i * freq / 10000) - self.a0 * np.sin(
                i * freq / 10000
            )

        ret.append(temp / samplePoints * 100)

    peaks, prop = find_peaks(ret, prominence=100, width=3, height=100, distance=40)
    prominence = prop["prominences"]

    for pr in range(len(prominence)):
        prominence[pr] /= 1000

    return [peaks, prominence]
bold scaffoldBOT
#

@merry kelp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

livid spindle
#

What is the problem?

merry kelp
#

Yo thanks

merry kelp
#

it doesn't work

livid spindle
#

which part doesn't work?

merry kelp
#

Everything?

#

I do have a commit that works

#

ish

#
import numpy as np


class DFT:
    def __init__(self, x: list):
        self.x: list = x
        self.T: int = len(x)
        self.a0: float | int = sum(x) / self.T

    def calc(self, freq: float | int, samplePoints: int):
        ret = []
        for i in range(self.T):
            ret.append(
                [
                    self.x[i] * np.sin(i * freq) - self.a0 * np.sin(i * freq),
                    self.x[i] * np.cos(i * freq) - self.a0 * np.cos(i * freq),
                ]
            )

        points = len(ret) // samplePoints

        tempX = 0
        tempY = 0
        for i in range(0, len(ret), points):
            tempX += ret[i][0]
            tempY += ret[i][1]

        tempX /= samplePoints
        tempY /= samplePoints

        dist = np.sqrt(tempX**2 + tempY**2)

        return [ret, tempX, tempY, dist]
#

The thing is

#

most of the code is in another file

#

so I wanted to compress it

#

I'm just gonna send the other part:```py
x = pygame.mouse.get_pos()[0]
t += inc
stepX = (x + 1) / (sizeX - sizeX / 8) * 100

dft = DFT(lst).calc(t, 2000)

newLst = dft[0]

xyLst.append(dft[1] * amplifier)

#

but this part has visualazation

#

I removed it

livid spindle
#

You know python has complex numbers build in?

#

!e py print(2 + 3j)

bold scaffoldBOT
merry kelp
#

Yes

#

But I don't need complex numbers

#

I only need the x axis

livid spindle
#

Well you used a list with the real and imag part instead and then added them element-wise.

merry kelp
#

Ayo, I just read your bio, you're pronoun quack, that's crazy. Admirable

#

sorry

livid spindle
#

But yes complex numbers aren't necessary.

merry kelp
#

The last thing I sent works

#

The thing is it's in two different files

#

I would like the DFT class to have like

#

the calc function would give you all the amplitudes, phases and frequanceies

livid spindle
#

Are you sure it's necessary to subtract a0?

merry kelp
#

yes

#

a0 is the avrage of the function I believe

livid spindle
#

Yes, but when you subtract it then the average can't be reconstructed from the result.

merry kelp
#

No I think it can

#

It's like

#

If you have a function that's very high

#

You just lower it down

#

you don't change anything

#

I'm sorry, I think Ima go, cya

livid spindle
#

good night

merry kelp
#

Night

bold scaffoldBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.