Mac OS X coding

Ditch the #define “constants”

April 4, 2007 · Leave a Comment

If you find yourself using a lot of #define X 10 (or other integers) for constants in your Objective-C code, stop! #define is parsed by the preprocessor and not the compiler(last I heard). It is more or less a search and replace of X with 10 by the preprocessor in the above example. It is not seen by the debugger or anything(since it is not a symbol). So, I would suggest enum.

enum values {first = 10} constant;

constant X = first;

This might be inconvenient if you only have a couple of #defines laying around, but it is better to use enums instead of large chunks of #define (even Apple uses enums ! — check the QuickTime 7.1 reference). Also, the author of Magic Number Machine (Matt Gallagher) uses enums extensively

Categories: Cocoa

Quadrature Amplitude Modulation

April 4, 2007 · 3 Comments

If you look back at my earlier post on QAM, you will notice that in the modulator(transmitter side), you will notice that we are multiplying one signal by \sin(50x) and the other by \cos(50x). These signals are generated by separating the original message signal into two signals, and then multiplying one by \sin(50x) and the other by \cos(50x). Now, in the previous post I used a sinusoidal message signal for illustration. In reality, message signals are generally NOT perfect sinusoids.

Without further ado, here is the explanation of QAM (formulation heavily adapted from Telecommunication Breakdown by Johnson&Sethares)

Let the message m(t) be separated into two signals m_1(t) and m_2(t). Then we modulate by multiplying one by \cos and the other by \sin. In other words, if v(t) is our modulated signal, v(t) is generated by the following:

v(t) = A_c ( m_1(t) \cos(2\pi f_c t) - m_2(t) \sin(2\pi f_c t) ). This is the signal transmitted. At the receiver, in the demodulater, we duplicated the signal received: call these x_1(t) and x_2(t). x_1(t) = v(t) \cos(2 \pi f_ct) =

A_c m_1(t) \cos^2(2 \pi f_c t) - A_c m_2(t) \sin(2 \pi f_c t) \cos(2 \pi f_c t) =

\frac{A_c m_1(t)}{2} (1 + \cos(4 \pi f_c t) ) - \frac{A_c m_2(t)}{2} ( \sin(4 \pi f_c t) ). Putting this signal through a lowpass filter(filters high frequencies) gives s_1(t) = \frac{A_c m_1(t)}{2}, and we have recovered the message.

Now, x_2(t) = v(t) \sin(2 \pi f_c t) = A_c m_1(t) \cos(2 \pi f_c t) \sin(2 \pi f_c t)

- A_c m_2(t) sin^2(2 \pi f_c t) = \frac{A_c m_1(t)}{2} \sin(4 \pi f_c t) - \frac{A_c m_2(t)}{2}(1 - \cos(4 \pi f_c t) ). With a lowpass filter, we get x_2(t) = \frac{-A_c m_2(t) }{2}

We can look at some IT++ code in the future

Categories: Telecommunications

On Regular Expressions

April 4, 2007 · Leave a Comment

(I originally published this post on my general blog).

Regular expressions are the alternative for full fledged parsers when there is lightweight parsing to do. For example they can be used to match a certain pattern in a file. They are available in all the scripting languages(Perl, Python, Ruby, Tcl, Shell scripting) and tacked on as a library in C++, Java, C#, etc. Here is an example in Ruby that finds floating-point numbers in a file(presumably for more processing with them).

Sample.txt has the following two lines:

#0 0.22 -3 -0.4
#0 5 3 7

Find.rb has the following content:

ary = Array.new
open(’sample.txt’).each do |line|
ary <</[-+]?[0-9]*\.[0-9]+/)
end

ary.each{|item| puts item }

Output of the code snippet is the following two lines
0.22
-0.4

You can check out my GPLed Regular Expression Tester here. Pay special attention to the disclaimers listed in the GPL as this little utility has NOT been tested very much.

Here is a language-neutral introduction to regular expressions(it also my reference for the post)

www.regular-expressions.info/

Categories: Cross-platform code · Ruby