Why does this code not return the correct fft frequencies?

33 Views Asked by At

I'm writing a code and I want to create a function that receives the amplitudes, frequencies and shifts and the points to be evaluated and returns a sum of sines with such amplitudes, frequencies and shifts on the points.

The code I used is:

import torch
import np

def sum_of_sines(weights, freqs, bias, scale, samples, period=2):
    x = torch.linspace(0, scale-1, samples)
    sines = torch.stack(
        [A * torch.sin(w * x * 2 * torch.pi / period + b) for (A,w,b) in zip(weights, freqs, bias)], 
        dim=1
        )
    return sines.sum(1)

The problem is, when I evaluate the function with integer frequencies like this

weights = [2., 1.]
freqs = [2., 4.]
bias = [0., 0.]
noise = sum_of_sines(weights, freqs, bias, 10, 1024)

and try to plot the frequencies in that function, it returns impulses on other frequencies and not on 2 and 4

fft = np.fft.fft(noise)
freqs = np.fft.fftfreq(len(noise), 1/1024)

fft freqs figure

Can someone help me understand where I got it wrong?

0

There are 0 best solutions below