Calculating amplitude from np.fft
I appear to be calculating incorrect amplitudes for the original waves using np.fft.fft.
The plot of the fft shown is shown, as you can see the amplitudes shown are around 3 and 1.5, but if you look at the code I'm using amplitudes 7 and 3 to generate the signal. This plot should have two spikes which go up to y=3 at x=13 and y=7 at x=15
What do I need to do to see the proper amplitudes (3 and 7) in my graph?
I can experimentally see the constant I need to multiply my amplitudes by is around 2.3, but how do I calculate this number exactly?
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 20
n_samples = 1000
xs = np.linspace(t0, t1, n_samples)
# Generate signal with amplitudes 7 and 3
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
np_fft = np.fft.fft(ys)
amplitudes = 1/n_samples * np.abs(np_fft) #This gives wrong results
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.plot(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()

python numpy
add a comment |
I appear to be calculating incorrect amplitudes for the original waves using np.fft.fft.
The plot of the fft shown is shown, as you can see the amplitudes shown are around 3 and 1.5, but if you look at the code I'm using amplitudes 7 and 3 to generate the signal. This plot should have two spikes which go up to y=3 at x=13 and y=7 at x=15
What do I need to do to see the proper amplitudes (3 and 7) in my graph?
I can experimentally see the constant I need to multiply my amplitudes by is around 2.3, but how do I calculate this number exactly?
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 20
n_samples = 1000
xs = np.linspace(t0, t1, n_samples)
# Generate signal with amplitudes 7 and 3
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
np_fft = np.fft.fft(ys)
amplitudes = 1/n_samples * np.abs(np_fft) #This gives wrong results
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.plot(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()

python numpy
1
you'll find information here : dsp.stackexchange.com/questions/16438/…
– Dadep
Jul 12 '18 at 6:54
@Dadep Thanks you're right, it works if I multiply by 2 and add more frequency bins.
– Keatinge
Jul 12 '18 at 7:02
add a comment |
I appear to be calculating incorrect amplitudes for the original waves using np.fft.fft.
The plot of the fft shown is shown, as you can see the amplitudes shown are around 3 and 1.5, but if you look at the code I'm using amplitudes 7 and 3 to generate the signal. This plot should have two spikes which go up to y=3 at x=13 and y=7 at x=15
What do I need to do to see the proper amplitudes (3 and 7) in my graph?
I can experimentally see the constant I need to multiply my amplitudes by is around 2.3, but how do I calculate this number exactly?
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 20
n_samples = 1000
xs = np.linspace(t0, t1, n_samples)
# Generate signal with amplitudes 7 and 3
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
np_fft = np.fft.fft(ys)
amplitudes = 1/n_samples * np.abs(np_fft) #This gives wrong results
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.plot(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()

python numpy
I appear to be calculating incorrect amplitudes for the original waves using np.fft.fft.
The plot of the fft shown is shown, as you can see the amplitudes shown are around 3 and 1.5, but if you look at the code I'm using amplitudes 7 and 3 to generate the signal. This plot should have two spikes which go up to y=3 at x=13 and y=7 at x=15
What do I need to do to see the proper amplitudes (3 and 7) in my graph?
I can experimentally see the constant I need to multiply my amplitudes by is around 2.3, but how do I calculate this number exactly?
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 20
n_samples = 1000
xs = np.linspace(t0, t1, n_samples)
# Generate signal with amplitudes 7 and 3
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
np_fft = np.fft.fft(ys)
amplitudes = 1/n_samples * np.abs(np_fft) #This gives wrong results
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.plot(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()

python numpy
python numpy
edited Jul 12 '18 at 6:32
Keatinge
asked Jul 12 '18 at 6:24
KeatingeKeatinge
3,03341330
3,03341330
1
you'll find information here : dsp.stackexchange.com/questions/16438/…
– Dadep
Jul 12 '18 at 6:54
@Dadep Thanks you're right, it works if I multiply by 2 and add more frequency bins.
– Keatinge
Jul 12 '18 at 7:02
add a comment |
1
you'll find information here : dsp.stackexchange.com/questions/16438/…
– Dadep
Jul 12 '18 at 6:54
@Dadep Thanks you're right, it works if I multiply by 2 and add more frequency bins.
– Keatinge
Jul 12 '18 at 7:02
1
1
you'll find information here : dsp.stackexchange.com/questions/16438/…
– Dadep
Jul 12 '18 at 6:54
you'll find information here : dsp.stackexchange.com/questions/16438/…
– Dadep
Jul 12 '18 at 6:54
@Dadep Thanks you're right, it works if I multiply by 2 and add more frequency bins.
– Keatinge
Jul 12 '18 at 7:02
@Dadep Thanks you're right, it works if I multiply by 2 and add more frequency bins.
– Keatinge
Jul 12 '18 at 7:02
add a comment |
1 Answer
1
active
oldest
votes
I think you are miscalculating the the amplitude. You should change the
amplitudes = 1/n_samples * np.abs(np_fft)
to
amplitudes = 2/n_samples * np.abs(np_fft)
result:
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 1
n_samples = 10000
xs = np.linspace(t0, t1, n_samples)
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
plt.subplot(2, 1, 1)
plt.plot(xs,ys)
np_fft = np.fft.fft(ys)
amplitudes = 2/n_samples * np.abs(np_fft)
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.subplot(2, 1, 2)
plt.semilogx(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()
The peaks of amplitudes are not exactly 7 and 2 but if you increase n_samples they will become more accurate.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51298604%2fcalculating-amplitude-from-np-fft%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you are miscalculating the the amplitude. You should change the
amplitudes = 1/n_samples * np.abs(np_fft)
to
amplitudes = 2/n_samples * np.abs(np_fft)
result:
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 1
n_samples = 10000
xs = np.linspace(t0, t1, n_samples)
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
plt.subplot(2, 1, 1)
plt.plot(xs,ys)
np_fft = np.fft.fft(ys)
amplitudes = 2/n_samples * np.abs(np_fft)
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.subplot(2, 1, 2)
plt.semilogx(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()
The peaks of amplitudes are not exactly 7 and 2 but if you increase n_samples they will become more accurate.
add a comment |
I think you are miscalculating the the amplitude. You should change the
amplitudes = 1/n_samples * np.abs(np_fft)
to
amplitudes = 2/n_samples * np.abs(np_fft)
result:
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 1
n_samples = 10000
xs = np.linspace(t0, t1, n_samples)
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
plt.subplot(2, 1, 1)
plt.plot(xs,ys)
np_fft = np.fft.fft(ys)
amplitudes = 2/n_samples * np.abs(np_fft)
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.subplot(2, 1, 2)
plt.semilogx(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()
The peaks of amplitudes are not exactly 7 and 2 but if you increase n_samples they will become more accurate.
add a comment |
I think you are miscalculating the the amplitude. You should change the
amplitudes = 1/n_samples * np.abs(np_fft)
to
amplitudes = 2/n_samples * np.abs(np_fft)
result:
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 1
n_samples = 10000
xs = np.linspace(t0, t1, n_samples)
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
plt.subplot(2, 1, 1)
plt.plot(xs,ys)
np_fft = np.fft.fft(ys)
amplitudes = 2/n_samples * np.abs(np_fft)
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.subplot(2, 1, 2)
plt.semilogx(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()
The peaks of amplitudes are not exactly 7 and 2 but if you increase n_samples they will become more accurate.
I think you are miscalculating the the amplitude. You should change the
amplitudes = 1/n_samples * np.abs(np_fft)
to
amplitudes = 2/n_samples * np.abs(np_fft)
result:
import numpy as np
import matplotlib.pyplot as plt
t0 = 0
t1 = 1
n_samples = 10000
xs = np.linspace(t0, t1, n_samples)
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)
plt.subplot(2, 1, 1)
plt.plot(xs,ys)
np_fft = np.fft.fft(ys)
amplitudes = 2/n_samples * np.abs(np_fft)
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)
plt.subplot(2, 1, 2)
plt.semilogx(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()
The peaks of amplitudes are not exactly 7 and 2 but if you increase n_samples they will become more accurate.
answered Nov 22 '18 at 15:52
FoadFoad
1,38821130
1,38821130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51298604%2fcalculating-amplitude-from-np-fft%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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

1
you'll find information here : dsp.stackexchange.com/questions/16438/…
– Dadep
Jul 12 '18 at 6:54
@Dadep Thanks you're right, it works if I multiply by 2 and add more frequency bins.
– Keatinge
Jul 12 '18 at 7:02