Category Archives: Floobydust

FFT of a Sampled Sine Wave (Revisited)

Excel is nice…but…

A better solution comes from the open-source tool Octave.  Here is the simple code to generate a sampled sine wave and perform an FFT.

myfile = fopen (“dat.txt”, “w”);
fprintf(myfile,”%f  %f\n”,0.0,0.0);
Samples = 1024;
OSR = 16;
length = 0;
z=0; v=0; vlp=0; zlp=0;
delt= 1; # delta time for RC calcultion
c=20; # capacitor for RC filter
r=1;  # resistor for RC filter
steps = Samples/OSR;
for i= 1:Samples+length
arg(i) = i – mod(i,steps);
arg(i) = 2*pi*arg(i)/Samples;
v(i) = sin(arg(i));
# smoothing function using digital averaging filter
#vlp(i)=0.0;
#if(i>length)
# for j=i-length:i
# vlp(i)=vlp(i)+v(j);
# endfor
# vlp(i) = vlp(i)/(length+1);
# endif
# end of smoothing filter
#RC filter function (Euler integration)
if(i>1)
ir(i)=(v(i)-vlp(i-1))/r;
vlp(i)=(delt*ir(i))/c + vlp(i-1);
endif
#end of RC filter function
# generate PWL data
if(mod(i,steps)==0)  #generates an output file for excel as needed
fprintf(myfile,”%fu  %f\n”,i-1,v(i-1));
fprintf(myfile,”%fu  %f\n”,i,v(i));
endif
endfor
plot(v);
pause(2)
plot(vlp);
pause(2)
y = fft(v);
z = 2*abs(y)/Samples;  #magnitude calculation
plot(z);
axis([0 100]);
pause(2)
ylp = fft(vlp);
zlp = 2*abs(ylp)/Samples;  #magnitude calculation
pause(2)
plot(zlp);
axis([0 100]);
fclose(myfile);

Picking a lock

I found this lock recently.  Had no idea what combination I had set, so I decided to figure out how to “unlock” the secret code!  It turned out to be quite easy.

20160220_164803-1

All you need is a good magnifying glass and enough light to illuminate the slot between the dials.

2016-02-20 16_56_26-lock.vsd - Microsoft Visio

FFT of a Sampled Sine Wave

I needed some graphics illustrating the frequency spurs of the output of a DAC when converting a sine wave.  The calculations of the spur frequencies is a function of the oversampling of the sinusoid and is trivial.  But, I needed graphics.  I thought about just drawing them in MSVisio but then decided that it would be fun to let Excel do the work for me.

So, I wrote a Visual Basic routine in Excel that first generated a sampled-data sinusoid, then calculated the FFT of that waveform and plots both the waveform and the FFT.  The parametric inputs for the calculations are the number of data points (must be power of 2), and the over sample ratio.  Again pretty simple stuff.

Here is an example output of a sampled sinusoid:

SampleSine

Here is the FFT output:

SampleFFT

Here is the VB code to do this:

Sub FFTDAC()
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Columns(“C:C”).Select
Selection.ClearContents
Pi = 3.1415926
Range(“H1”).Offset(0, 0).Select
samples = Selection.Offset(0, 0).Value
OSR = Selection.Offset(1, 0).Value
inc = samples / OSR
Range(“A1”).Offset(0, 0).Select
For i = 1 To samples
arg = i – i Mod inc
arg = arg * 2 * Pi / samples
Value = Sin(arg)
Selection.Offset(0, 0).Value = Value
Selection.Offset(1, 0).Select
Next i
Range(“D1”).Offset(0, 0).Select
For i = 1 To samples
cellname = “C” & i
fcn = “=ImAbs(” + cellname + “)”
Selection.Offset(0, 0).Value = fcn
Selection.Offset(1, 0).Select
Next i

Application.Run “ATPVBAEN.XLAM!Fourier”, ActiveSheet.Range(“$A$1:$A$128”), _
ActiveSheet.Range(“$C$1”), False, False
End Sub

In order for this macro to work, you have to install/enable the Analysis ToolPak and Analysis ToolPak VBA

VBA setup