# Why I am not getting the same frequency when sampling a waveform? [duplicate]

This question already has an answer here:

I am trying to simulate the sampling of a waveform to then apply FFT Analysis to it. Here is the sample code I have now using a square wave:

frequency =50;
sampleFrec = 1000;
period =1/frequency;

f=\[Piecewise]  -1  (period/2)<=Mod[x,period]<period
1  True

testData=Table[N@f,{x,0,2*period,1/sampleFrec}];

Plot[f,{x,0,2*period}, PlotLabel->"Fundamental",ExclusionsStyle->Dashed, PlotStyle->{Thickness[0.006], Red}]

ListLinePlot[testData,PlotRange->Full, PlotLabel->"Fundamental"]


And this are the plots I'm getting, the first one is the 50Hz square wave I want but when I use Table[] and ListLinePlot[] I am not getting the same frequency, why? I am relatively new to Wolfram Mathematica so maybe this is something very stupid. Thank you for your help!

## marked as duplicate by Mr.Wizard♦Jun 25 '14 at 17:01

• Hello Andres. In this case you merely need to add DataRange -> {0, 2*period} to ListPlot. – Mr.Wizard Jun 25 '14 at 16:21
• Nice! Why this happens? The same doesn't happen with Plot[] – Andres Jun 25 '14 at 16:27
• ListLinePlot has no way of knowing what range your data is supposed to cover, so it simply uses the number of data points as the range. – Mr.Wizard Jun 25 '14 at 16:31