Discrete Fourier transform of sampled sin function nothing like continuous?












0












$begingroup$


So I'm a little confused about what is going on with the discrete Fourier transform.



I tested out discrete Fourier transform with a little python script on a sin function



import os
import numpy as np

import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.linspace(0, 20, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


fig, ax = plt.subplots()
ax.plot(g)


I was expecting a single blip at 1 but instead I get something thats non zero everywhere.
enter image description here



What is going on?



Edit:



I did not use linspace correctly, if I wanted $n in 0 ldots 20 $ then I should have done linspace(0,20, 21) for that. Anyway, If I instead do this



x = np.linspace(0, 19, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


Now I have 20 samples, and a sin of angular frequency $frac{2 pi}{20}$



and I get this plot
enter image description here



Which is better because its almost zero in the middle, but there is still this strange behavior where it dips negative around the 0th frequency. Also shouldn't there be 2 peaks because the complex part needs to be cancelled? And isn't the zeroth frequency corresponding to $e^0$?



Edit 2:



I have literally no idea what changed but it works now. Firstly, I was supposed too be getting zero when I plotted g because the fft of a real sin is only imaginary, so idk how I was even getting a real fft to begin with. Anyway, that's gone now. Nothing in my code is different from the second edit.



When I multiply the fft by -1j I get this graph nowenter image description here
which is correct










share|cite|improve this question











$endgroup$












  • $begingroup$
    Try x = np.linspace(0, 20, 20, endpoint=False).
    $endgroup$
    – Rahul
    Jan 2 at 8:10












  • $begingroup$
    So you are computing the FFT of $y(n) = sin(2pi n/20)$, $n in 0 ldots 20$ not $n in 0 ldots 19$. Since $20$ isn't a multiple of $21$ you get aliasing when periodizing the FT of $sin(2pi t/20)$.
    $endgroup$
    – reuns
    Jan 2 at 12:18












  • $begingroup$
    Ok I fixed that, check out my edit
    $endgroup$
    – Chubmaster25
    Jan 2 at 17:52










  • $begingroup$
    Use a for loop to create y instead of loosing your time with linspace. With $y(n) = sin(2pi n/20),n in 0 ldots 19$ then its FFT will be two peaks at $k =1, 19$ (proof : $2isin(x) = e^{ix}-e^{-ix}$ then geometric sum)
    $endgroup$
    – reuns
    Jan 2 at 19:28












  • $begingroup$
    ok I got a correct answer check my edit. idk how I even got a graph for g when the fourier transform is supposed to be only imaginary.
    $endgroup$
    – Chubmaster25
    Jan 2 at 23:10
















0












$begingroup$


So I'm a little confused about what is going on with the discrete Fourier transform.



I tested out discrete Fourier transform with a little python script on a sin function



import os
import numpy as np

import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.linspace(0, 20, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


fig, ax = plt.subplots()
ax.plot(g)


I was expecting a single blip at 1 but instead I get something thats non zero everywhere.
enter image description here



What is going on?



Edit:



I did not use linspace correctly, if I wanted $n in 0 ldots 20 $ then I should have done linspace(0,20, 21) for that. Anyway, If I instead do this



x = np.linspace(0, 19, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


Now I have 20 samples, and a sin of angular frequency $frac{2 pi}{20}$



and I get this plot
enter image description here



Which is better because its almost zero in the middle, but there is still this strange behavior where it dips negative around the 0th frequency. Also shouldn't there be 2 peaks because the complex part needs to be cancelled? And isn't the zeroth frequency corresponding to $e^0$?



Edit 2:



I have literally no idea what changed but it works now. Firstly, I was supposed too be getting zero when I plotted g because the fft of a real sin is only imaginary, so idk how I was even getting a real fft to begin with. Anyway, that's gone now. Nothing in my code is different from the second edit.



When I multiply the fft by -1j I get this graph nowenter image description here
which is correct










share|cite|improve this question











$endgroup$












  • $begingroup$
    Try x = np.linspace(0, 20, 20, endpoint=False).
    $endgroup$
    – Rahul
    Jan 2 at 8:10












  • $begingroup$
    So you are computing the FFT of $y(n) = sin(2pi n/20)$, $n in 0 ldots 20$ not $n in 0 ldots 19$. Since $20$ isn't a multiple of $21$ you get aliasing when periodizing the FT of $sin(2pi t/20)$.
    $endgroup$
    – reuns
    Jan 2 at 12:18












  • $begingroup$
    Ok I fixed that, check out my edit
    $endgroup$
    – Chubmaster25
    Jan 2 at 17:52










  • $begingroup$
    Use a for loop to create y instead of loosing your time with linspace. With $y(n) = sin(2pi n/20),n in 0 ldots 19$ then its FFT will be two peaks at $k =1, 19$ (proof : $2isin(x) = e^{ix}-e^{-ix}$ then geometric sum)
    $endgroup$
    – reuns
    Jan 2 at 19:28












  • $begingroup$
    ok I got a correct answer check my edit. idk how I even got a graph for g when the fourier transform is supposed to be only imaginary.
    $endgroup$
    – Chubmaster25
    Jan 2 at 23:10














0












0








0





$begingroup$


So I'm a little confused about what is going on with the discrete Fourier transform.



I tested out discrete Fourier transform with a little python script on a sin function



import os
import numpy as np

import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.linspace(0, 20, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


fig, ax = plt.subplots()
ax.plot(g)


I was expecting a single blip at 1 but instead I get something thats non zero everywhere.
enter image description here



What is going on?



Edit:



I did not use linspace correctly, if I wanted $n in 0 ldots 20 $ then I should have done linspace(0,20, 21) for that. Anyway, If I instead do this



x = np.linspace(0, 19, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


Now I have 20 samples, and a sin of angular frequency $frac{2 pi}{20}$



and I get this plot
enter image description here



Which is better because its almost zero in the middle, but there is still this strange behavior where it dips negative around the 0th frequency. Also shouldn't there be 2 peaks because the complex part needs to be cancelled? And isn't the zeroth frequency corresponding to $e^0$?



Edit 2:



I have literally no idea what changed but it works now. Firstly, I was supposed too be getting zero when I plotted g because the fft of a real sin is only imaginary, so idk how I was even getting a real fft to begin with. Anyway, that's gone now. Nothing in my code is different from the second edit.



When I multiply the fft by -1j I get this graph nowenter image description here
which is correct










share|cite|improve this question











$endgroup$




So I'm a little confused about what is going on with the discrete Fourier transform.



I tested out discrete Fourier transform with a little python script on a sin function



import os
import numpy as np

import matplotlib.pyplot as plt
import matplotlib as mpl

x = np.linspace(0, 20, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


fig, ax = plt.subplots()
ax.plot(g)


I was expecting a single blip at 1 but instead I get something thats non zero everywhere.
enter image description here



What is going on?



Edit:



I did not use linspace correctly, if I wanted $n in 0 ldots 20 $ then I should have done linspace(0,20, 21) for that. Anyway, If I instead do this



x = np.linspace(0, 19, 20)

y = np.zeros(20)
y[:] = np.sin(2*np.pi*x/20)

g = np.fft.fft(y)


Now I have 20 samples, and a sin of angular frequency $frac{2 pi}{20}$



and I get this plot
enter image description here



Which is better because its almost zero in the middle, but there is still this strange behavior where it dips negative around the 0th frequency. Also shouldn't there be 2 peaks because the complex part needs to be cancelled? And isn't the zeroth frequency corresponding to $e^0$?



Edit 2:



I have literally no idea what changed but it works now. Firstly, I was supposed too be getting zero when I plotted g because the fft of a real sin is only imaginary, so idk how I was even getting a real fft to begin with. Anyway, that's gone now. Nothing in my code is different from the second edit.



When I multiply the fft by -1j I get this graph nowenter image description here
which is correct







fourier-analysis fast-fourier-transform






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Jan 2 at 23:09







Chubmaster25

















asked Jan 2 at 8:00









Chubmaster25Chubmaster25

243




243












  • $begingroup$
    Try x = np.linspace(0, 20, 20, endpoint=False).
    $endgroup$
    – Rahul
    Jan 2 at 8:10












  • $begingroup$
    So you are computing the FFT of $y(n) = sin(2pi n/20)$, $n in 0 ldots 20$ not $n in 0 ldots 19$. Since $20$ isn't a multiple of $21$ you get aliasing when periodizing the FT of $sin(2pi t/20)$.
    $endgroup$
    – reuns
    Jan 2 at 12:18












  • $begingroup$
    Ok I fixed that, check out my edit
    $endgroup$
    – Chubmaster25
    Jan 2 at 17:52










  • $begingroup$
    Use a for loop to create y instead of loosing your time with linspace. With $y(n) = sin(2pi n/20),n in 0 ldots 19$ then its FFT will be two peaks at $k =1, 19$ (proof : $2isin(x) = e^{ix}-e^{-ix}$ then geometric sum)
    $endgroup$
    – reuns
    Jan 2 at 19:28












  • $begingroup$
    ok I got a correct answer check my edit. idk how I even got a graph for g when the fourier transform is supposed to be only imaginary.
    $endgroup$
    – Chubmaster25
    Jan 2 at 23:10


















  • $begingroup$
    Try x = np.linspace(0, 20, 20, endpoint=False).
    $endgroup$
    – Rahul
    Jan 2 at 8:10












  • $begingroup$
    So you are computing the FFT of $y(n) = sin(2pi n/20)$, $n in 0 ldots 20$ not $n in 0 ldots 19$. Since $20$ isn't a multiple of $21$ you get aliasing when periodizing the FT of $sin(2pi t/20)$.
    $endgroup$
    – reuns
    Jan 2 at 12:18












  • $begingroup$
    Ok I fixed that, check out my edit
    $endgroup$
    – Chubmaster25
    Jan 2 at 17:52










  • $begingroup$
    Use a for loop to create y instead of loosing your time with linspace. With $y(n) = sin(2pi n/20),n in 0 ldots 19$ then its FFT will be two peaks at $k =1, 19$ (proof : $2isin(x) = e^{ix}-e^{-ix}$ then geometric sum)
    $endgroup$
    – reuns
    Jan 2 at 19:28












  • $begingroup$
    ok I got a correct answer check my edit. idk how I even got a graph for g when the fourier transform is supposed to be only imaginary.
    $endgroup$
    – Chubmaster25
    Jan 2 at 23:10
















$begingroup$
Try x = np.linspace(0, 20, 20, endpoint=False).
$endgroup$
– Rahul
Jan 2 at 8:10






$begingroup$
Try x = np.linspace(0, 20, 20, endpoint=False).
$endgroup$
– Rahul
Jan 2 at 8:10














$begingroup$
So you are computing the FFT of $y(n) = sin(2pi n/20)$, $n in 0 ldots 20$ not $n in 0 ldots 19$. Since $20$ isn't a multiple of $21$ you get aliasing when periodizing the FT of $sin(2pi t/20)$.
$endgroup$
– reuns
Jan 2 at 12:18






$begingroup$
So you are computing the FFT of $y(n) = sin(2pi n/20)$, $n in 0 ldots 20$ not $n in 0 ldots 19$. Since $20$ isn't a multiple of $21$ you get aliasing when periodizing the FT of $sin(2pi t/20)$.
$endgroup$
– reuns
Jan 2 at 12:18














$begingroup$
Ok I fixed that, check out my edit
$endgroup$
– Chubmaster25
Jan 2 at 17:52




$begingroup$
Ok I fixed that, check out my edit
$endgroup$
– Chubmaster25
Jan 2 at 17:52












$begingroup$
Use a for loop to create y instead of loosing your time with linspace. With $y(n) = sin(2pi n/20),n in 0 ldots 19$ then its FFT will be two peaks at $k =1, 19$ (proof : $2isin(x) = e^{ix}-e^{-ix}$ then geometric sum)
$endgroup$
– reuns
Jan 2 at 19:28






$begingroup$
Use a for loop to create y instead of loosing your time with linspace. With $y(n) = sin(2pi n/20),n in 0 ldots 19$ then its FFT will be two peaks at $k =1, 19$ (proof : $2isin(x) = e^{ix}-e^{-ix}$ then geometric sum)
$endgroup$
– reuns
Jan 2 at 19:28














$begingroup$
ok I got a correct answer check my edit. idk how I even got a graph for g when the fourier transform is supposed to be only imaginary.
$endgroup$
– Chubmaster25
Jan 2 at 23:10




$begingroup$
ok I got a correct answer check my edit. idk how I even got a graph for g when the fourier transform is supposed to be only imaginary.
$endgroup$
– Chubmaster25
Jan 2 at 23:10










0






active

oldest

votes












Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3059229%2fdiscrete-fourier-transform-of-sampled-sin-function-nothing-like-continuous%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematics Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3059229%2fdiscrete-fourier-transform-of-sampled-sin-function-nothing-like-continuous%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen