What to do for the right FFT in Matlab (two peaks and incorrect amplitude)?

This is the FFT I'm using. But two peaks (did not expect the one in the beginning) are occurring and the amplitude is not as specified (expecting a value of 2). Any help is much appreciated!

Amp = 2;
freqHz = 10000;
fsHz = freqHz*2+1;
dt = 1/fsHz;

sine = Amp*sin(2*pi*freqHz*(0:dt:1-dt));

transform = fft(sine,fsHz)/fsHz;
magTransform = abs(transform);

faxis = linspace(0,fsHz/2,fsHz);
plot(faxis,fftshift(magTransform));
xlabel('Frequency')


• Please clarify what your problem is: how many peaks did you expect (and why) vs how many you're getting; and what amplitude did you expect (and why) vs what you're actually getting. Do not add this information as a comment; rather, edit your question.
– MBaz
Jun 26, 2018 at 17:09
• Please see the picture I've uploaded Jun 26, 2018 at 19:00

You need to use complex signal for that. Use this

Amp = 2; freqHz = 10000; fsHz = freqHz*2+1; dt = 1/fsHz; sine = Amp*exp(2i*pi*freqHz*(0:dt:1-dt)); transform = fft(sine,fsHz)/fsHz; magTransform = abs(transform); faxis = linspace(-fsHz/2,fsHz/2,fsHz); plot(faxis,fftshift(magTransform)); xlabel('Frequency')

The somehow symeetric aspect of the complex DFT might be misleading, so in the past I shared a simple Matlab function FFTR.m for real signals only, that might yield the result you'd expect ($2$ magnitude at peak frequency):

as long as you don't try bordeline endeavours. That requires a little normalization and a special care at DC and Nyquist. The code could be:

% Signal definition
Amp = 2;
freqHz = 10000;
fsHz = freqHz*2+1;
dt = 1/fsHz;
sine = Amp*sin(2*pi*freqHz*(0:dt:1-dt));

% Graphic part
[fftR,fftAxe] = FFTR(sine',dt);
plot(fftAxe,fftR,'o-');
xlabel('Frequency')
ylabel('Magnitude')

• Thanks! How can I achieve this with the built in function? Jun 26, 2018 at 19:02
• Just explore the FFT.m link I gave as a link, and steal the code :) Jun 26, 2018 at 19:03
• And if you like it, don't hesitate to upvote! Jun 30, 2018 at 11:38
• Unfortunately my rep is not sufficient :/ Jul 1, 2018 at 11:44