Fourier transformation for cosine curves

tf.fft requires complex data types for its arguments. However, the cosine curve we are discussing here only has real values in its sequence. We aren't interested in the imaginary part of the input this time because we aim to draw the curve in the real 2D space.

The best way to deal with this situation is by padding the imaginary part with zero. tf.zerosLike gives us a tensor whose shape is exactly the same as the input tensor, with all zero values:

const input = tf.complex(ys, ys.zerosLike());

Thus, we get the input tensor with an imaginary part, zero. This will be the N-dimensional vector whose length is 30:

The usage of FFT is simple. All we need to do is pass the vector to tf.fft:

const transformed ...

Get Hands-On Machine Learning with TensorFlow.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.