Tuesday, November 30, 2010

Discrete Fourier Transform in Ruby

...Just for a laugh...

I'm reading this amazing book at the moment, http://www.dspguide.com/ and thought it would be fun to code some stuff in Ruby.

The code...


require 'complex'

class DFT
        def initialize( size )
                @freq = Array.new( size, Complex(0,0) )

        # set data
        def data( data )
                @data = data

        # get frequency table
        def freq

        # reset the frequency table
        def reset
                @freq.map! do |d|
                        Complex( 0,0 )

        # transform data and return the frequency table
        def transform
                raise "No input data" unless @data
                (0..@freq.size-1).each do |k|
                        @data.each_with_index do |d,i|
                                @freq[k] = Complex(
                                        @freq[k].real + d * Math.cos( 2 * Math::PI * k * i / @data.size ),
                                        @freq[k].imag - d * Math.sin( 2 * Math::PI * k * i / @data.size )
                return @freq

And a test...


require 'dft.rb'
require 'test/unit'

class TestDFT < Test::Unit::TestCase
        def setup
                # Compose a data set containing two frequencies, f1 & f2
                # Assume the data set contains 1 second worth of samples so
                # f1 & f2 are in Hz and should be less than half the number of samples (Nyquist)
                @data = Array.new(64)
                f1 =10
                f2 =25
                amplitude = 1
                @data.map! do |d|
                        Math.sin( 2 * Math::PI * f1 * i / @data.size ) + Math.sin( 2 * Math::PI * f2 * i / @data.size )

        def test_dft_throws_exception_with_no_input
                dft = DFT.new( 32 )
                assert_raise RuntimeError do
                        freq = dft.transform

        def test_dft_has_correct_output_size
                dft = DFT.new( 32 )
                dft.data( @data )
                freq = dft.transform
                assert_equal( 32, freq.size )
                assert_equal( 32, dft.freq.size )

        def test_dft_has_correct_output
                dft = DFT.new( 32 )
                dft.data( @data )

                assert dft.freq.at(10).abs > 1
                assert dft.freq.at(25).abs > 1

        def test_display
                dft = DFT.new( 32 )
                dft.data( @data )
                puts "\nFrequencies found:\n"
                dft.freq.each_with_index do |f,i|
                        if (f.abs > 1)
                                printf( "%d %.30f   %.30f  %.30f\n", i, f.real, f.imag, f.abs )

Wednesday, November 17, 2010

Unit testing in TCL

The unit...


proc fib {n} {
        if {$n==1}  {return $n}
        if {$n==2}  {return [expr {$n-1}]}
        if {$n==3}  {return [expr {$n-1}]}
        return [expr { [fib [expr {$n - 1}]] + [fib [expr {$n - 2}]] } ]

The test...


package require tcltest 2.0
namespace import ::tcltest::*
source ../src/fib.tcl

# 1 1 2 3 5 8 13 21 34

test fib-1 { Test first is 1 } {
        fib 1
} 1

test fib-2 { Test second is 1 } {
        fib 2
} 1

test fib-3 { Test third is 2 } {
        fib 3
} 2

test fib-4 { Test fourth is 3 } {
        fib 4
} 3

test fib-5 { Test fifth is 5 } {
        fib 5
} 5

test fib-6 { Test ninth is 34 } {
        fib 9
} 34

test fib-7 { Test fortieth is 102334155 } {
        fib 40
} 102334155 

I put in the last couple of tests for good luck and for #ian :-)

Oh I must have been bored....


Tuesday, November 16, 2010

The Six Characteristics of Shorinji Kempo

Todays T-Shirt choice reminded me of the Six Characteristics of Shorinji Kempo.

  • Ken Zen Ichinyo – Body and the mind are one.
  • Riki Ai Funi – Strength and power should never be separated from compassion.
  • Shushu Koju – Defence comes before attack. 
  • Fusastu Katsujin – Defend and protect without killing. 
  • Goju Ittai – Use hard and soft techniques together, in balance.
  • Kumite Shutai – Pair work is fundamental.  


Live Half for yourself, Half for Others.


That is all.

Friday, November 12, 2010

Homebrew tapeless camera

This is an ongoing project I'm working on which started as a circuit that I decided was necessary to be able to neatly interface the nNovia QC120 A2D2 recorder to a Sony DXC-325P camera with CA-325P studio back.
This interface deals with translating VTR start/stop signals from the camera into GPI signals to pause and resume the recorder when it is in record mode.

In addition to the device control, there will also be XLR Balanced line input to be converted to unbalanced line level.
There is also the possibility of getting it to communicate with the nNovia RS-422 port and using this protocol to control the device.
I'm still working on the final design. I have built the PIC 16f872 based circuit that controlls the nNovia via the GPI control lines and this seems to work very well.
The next step with the PIC interface is to try controlling the RS232 port on the nNovia to start and stop the deck and get its status.
I will be prototyping the Preamp shortly. I have a plan to use the A2D interfaces on the PIC to drive an LED PPM or VU meter on the front panel of the case.

Here's the schematic of the current prototype.

Thursday, November 11, 2010

Is TDD Karate or Kung-fu?

Today we had a discussion in the office about the current TDD (Test Driven Development) course we are following and it has to be said there is more than a little confusion over it. One of the main sources of this confusion seems to be 'why' we are doing it.  And I can see where the confusion creeps in. Quite a few of the members of the team have years of programming experience and may already or have been practicing TDD in some guise or another so those people already know the benefits and have adapted the process to their way of working. To me this is fine. Perhaps they would be better served with working on some other very useful principles such as the SOLID principles. [Robert C. Martin] Some of the other members know of TDD and are perhaps reluctant to use it because of past experiences or whatever.  These people need to know what the goal is, what will they be good at at the end of this?

At this point someone started explaining that it was like learning Karate. In some martial arts including Karate it is common for the students to practice set moves or Kata by copying the seniors in the group but in the beginning it appears (Karate experts please correct me) that they are not necessarily told why they perform the moves they do, just to do them. Remember, polish-on, polish-off from Karate kid?

Well, I don't subscribe to this opinion. I have practiced martial arts and the one I happened to choose, Shorinji Kempo, (Shorinji being the Japanese form of Shaolin) teaches it's students useful techniques and the reasons for using them. And it teaches all aspects of the art continuously and with balance gradually increasing in complexity. At no point does anyone not know why they are doing something.  The art also encourages strategic thinking and how to choose the best technique to use for a given situation (even if the best strategy is not to get involved at all).

I think that this would be a better way to progress and learn the techniques, especially for the enquiring minds. Learning by rote has no interest for me.

So do you want to be a Karate black belt beating bricks with your bare hands (why do they do that?) or a contemplative Shaolin monk analysing the situation and choosing the best strategy to approach it.


[Robert C. Martin, SOLID Principles, http://en.wikipedia.org/wiki/Solid_(object-oriented_design) ]

ps. I know I mixed up Kung-fu, Shorinji Kempo etc.. but you get the point.. maybe.

pps. In fact I wonder if I mean 'Martial Art' vs 'Martial Way' aka Budo? (Was reminded of Aikido   by @Boston_IT)