Without further information there is not much we can do to answer it. I cannot post comments yet, so for the second image I will post an answer:
If you sample your signal with 1kHz, according to the Nyquist-Shannon theorem, you can only have frequencies of - to 500Hz in your signal or otherwise you will get aliasing and therefore images of the signal at frequency multiples of your sampling rate.
If your signal is at 980Hz and you sample with 1000Hz, the images will show at 1000-980 Hz, 980-1000, 2000-980, 980-2000 and so on. That is the reason you appear to have energy around 20 Hz in your signal. To get the result you want, you either have to increase the sampling frequency by the factor of two or you need to change the signal to contain only an absolute maximum frequency of 500Hz.
Regarding the first image: It looks like those are caused by summing up sines and cosines to create a rectangle as done with the Fourier series. But like said above: Without further information, there's not much to help you there.