I have wasted to much time trying to write a DFT algoritm in C, source code can be found here. It's not very good, but it computes correctly. Here is an example, where I put in a noisy signal, it contains three sine signals, with frequencies 2Hz, 17Hz and 401Hz with different amplitudes and some noise. It looks like this
And after the Fourier Transform i get this data
In the code I simply checked which frequencies was larger than 0.35 in amplitude, and saved them. The other ones was zeroed. After and inverse fourier transform with the ifft() function. The output was this.
Pretty :)
Matthew 6:34
"Therefore do not be anxious about tomorrow,
for tomorrow will be anxious for itself.
Sufficient for the day is its own trouble."
Thursday, 31 May 2012
Tuesday, 8 May 2012
Discrete low-pass filter in C
I was interested in trying to make a discrete low-pass filter in C, also make it so that it can take data from from the pipe maybe. Anyway, the filter_rc() function can take data by keep pushing in new values and it takes care of everything.
One problem that my program has is that it cannot run forever, it can only take a limited amount of samples till the buffer is full. I think it is possible to check the value with the first index in the buffer (the last_value[] buffer), and if it is zero the buffer can shift down, I think this will work. Since the zero values in the sum isn't necessary anymore.
Here is a plot of the output, the input is a Sine Wave with 1 Amplitude, and 42Hz. It's plotted over 0.1s. And the filter is C=4.7uF, R=5k. The program can take any input, it doesn't have to be a sine wave. The red curve has 20 samples over 0.1s, and the green has 100 samples over 0.1s.
The Program can be found here
https://github.com/Spekkio/dig_filter/blob/master/main.c
One problem that my program has is that it cannot run forever, it can only take a limited amount of samples till the buffer is full. I think it is possible to check the value with the first index in the buffer (the last_value[] buffer), and if it is zero the buffer can shift down, I think this will work. Since the zero values in the sum isn't necessary anymore.
Here is a plot of the output, the input is a Sine Wave with 1 Amplitude, and 42Hz. It's plotted over 0.1s. And the filter is C=4.7uF, R=5k. The program can take any input, it doesn't have to be a sine wave. The red curve has 20 samples over 0.1s, and the green has 100 samples over 0.1s.
Sunday, 29 April 2012
Low-Pass filter math
I was trying to calculate the curve of a simple low-pass filter when feeding it with a square-wave signal, for example from a PWM. Later I also made some equations where duty cycle was a variable, but they are maybe lost now or still saved somewhere... There are some complicated laplace transforms in this, I used Wolfram Alpha for making the laplace transforms, it would take too much time for me to solve manually :)
Calculations below are locked to 50% Duty Cycle.
The square wave signal can be represented with a function in time like this.
Here is an example of a curve with some iterations in the sum, and frequency 120Hz.
The Circuit looks like this
And the output of the filter is calculated like this, represented in the complex plane.You should understand what this equation means, otherwise the rest will be pretty cryptic :P
Now we can laplace transform the square wave signal, and replace that with Vin in the formula above.
First I take the the inside of the sum from the square wave function.
And also this
Now, for the inverse laplace transform, take the laplace result from above and replace with Vout in the low-pass filter equation. This is complicated :)
This results in:
And we will not forget the small sum in the beginning, that can be calculated separetely.
This becomes a long and complicated function of time, this could maybe be simplified.
Here is an example, displaying the input square wave, and the output signal, the circuit is C=47uF, R=5k and the frequency is 120Hz square signal from 0-5V.
Calculations below are locked to 50% Duty Cycle.
The square wave signal can be represented with a function in time like this.
Here is an example of a curve with some iterations in the sum, and frequency 120Hz.
The Circuit looks like this
And the output of the filter is calculated like this, represented in the complex plane.You should understand what this equation means, otherwise the rest will be pretty cryptic :P
Now we can laplace transform the square wave signal, and replace that with Vin in the formula above.
First I take the the inside of the sum from the square wave function.
And also this
Now, for the inverse laplace transform, take the laplace result from above and replace with Vout in the low-pass filter equation. This is complicated :)
This results in:
And we will not forget the small sum in the beginning, that can be calculated separetely.
This becomes a long and complicated function of time, this could maybe be simplified.
Here is an example, displaying the input square wave, and the output signal, the circuit is C=47uF, R=5k and the frequency is 120Hz square signal from 0-5V.
Friday, 27 April 2012
Subscribe to:
Posts (Atom)









