Category Archives: Telecommunications

Boost Smart Pointers or: How I learned to Stop Worrying and Love C++

Boost smart pointers fix many of the things wrong with auto_ptr. The smart pointer library consists of

scoped_ptr <boost/scoped_ptr.hpp> Simple sole ownership of single objects. Noncopyable.
scoped_array <boost/scoped_array.hpp> Simple sole ownership of arrays. Noncopyable.
shared_ptr <boost/shared_ptr.hpp> Object ownership shared among multiple pointers
shared_array <boost/shared_array.hpp> Array ownership shared among multiple pointers.
weak_ptr <boost/weak_ptr.hpp> Non-owning observers of an object owned by shared_ptr.
intrusive_ptr <boost/intrusive_ptr.hpp> Shared ownership of objects with an embedded reference count.

The table is courtesy of the link here.

I will just go through a simple example here. For a slightly more detailed look, you can take a look at this article.

Installing Boost on Mac OS X

Compiling and installing Boost is straightforward even if it takes some time and is processor intensive–it kept both cores of my Intel Core Duo pretty busy. What you really should do is follow the more authoritative guide from the Boost folks here. Here is my summary. Disclaimer: This is not a comprehensive guide. I am not liable for any failures you might have. See the link above for a more detailed guide.

  1. Install bjam (it is a tool like make, but supposedly better). Put bjam in your PATH
  2. Invoke bjam. Step 3 shows what an invocation might look like(you can customize this to your preferences): The following command works if you have the boost_1_34_0 directory in ~/ and bjam built and set up correctly.
  3. $ cd ~/boost_1_34_0
  4. $ bjam --build-dir=/tmp/build-boost --toolset=darwin stage
  5. After a while, type in
    echo $?

    to make sure everything proceeded correctly.

  6. Try the example given on the Boost page(see here).

A smart pointer example.

I’ll be using the IT++ library for this example. Of course, you can make your own examples without the library. The concepts are what matter.

typedef boost::scoped_ptr<PAM> pam;

int main()
{
pam p(new PAM(2));

vec output;

bvec demodded_out;

bvec input = “0100100001100101011011000110110001101111001000000101011101101111011

10010011011000110010000100001″;

//The following line of code simulates the transmitter end.
p->modulate_bits(input,output);

//The following simulates the receiver end.
p->demodulate_bits(output,demodded_out);
if(demodded_out == input)
{
cout<<“Success”;
}
else {
cout<<“Failure”<<endl;
}
return 0;
}

This is basically my previous code, with some smart pointer stuff added in. Notice that I didn’t deallocate any memory(I didn’t have to).

Advertisements

An FIR filter in C

I came across a nice article the other day. The company, Netrino, actually has lots of good articles. This was the code fragment(doesn’t compile without some adding some straighforward declarations and definitions, etc.) in the article(without the comments)
sample = input();
x[oldest] = sample;
y = 0;
for (k = 0; k < N; k++)
y += h[k] * x[(oldest + k) % N];
oldest = (oldest + 1) % N;
output(y);

This is fine for illustration(which was what this article was doing). In a real-time DSP environment though, you would never use the modulus operator(too computationally expensive). Instead:

x[oldest] = sample;
y = 0.0;
for (k = 0; k < N; k++)
{
if( (oldest + k) < N )
y += h[k] * x[(oldest + k)];
else {
y += h[k] * x[(oldest + k)-N];
}
}

if((oldest + 1) < N)
oldest += 1;
else
oldest = 0;
output(y);

In fact, even the built-in sin, cos etc. of the C math library are not used. Instead lookup tables are used.

Exploring Fourier Series with Ruby

There is scope for refactoring in this port(such as replacing each with more powerful methods). My first priority was to get it working. Now that it does, I will refactor the code sometime soon, hopefully.

I ported the code from the Python for Fun series’ article entitled Waves and Harmonics. This port still has some rough edges, no pun intended, and not tested very well(see the disclaimers below). It is located on the Box widget to your left with the label ploty.rb. It can be run by typing in ruby ploty.rb. The code is licensed under GPLv2

To actually “explore the Fourier series”, please go ahead and read the original article above. I don’t have the time and patience to go into that after porting this thing–the Ruby/Tk canvas binding was not that intuitive to me(which is rare, most ruby packages I have run into are very intuitive). I had to hunt and peck through Google to find out more about TkcLine, etc.

It turns out, that this is a good collection of scripts–just source code–mostly English, some Japanese strings are there sometimes:

http://lecture.ecc.u-tokyo.ac.jp/~shagiya/progs/

Here’s a screenshot

picture-1.png

Unfortunately, the Mac version doesn’t take advantage of anti-aliased rendering.

NO WARRANTY

  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.

  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.

Quadrature Amplitude Modulation

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

Big Nerd Ranch on NPR and thoughts on HD Audio

NPR covered Big Nerd Ranch for a few minutes today. The NPR guy talked about how the programmers were talking about the Spotlight API.

On an unrelated note:

Note: I am not necessarily talking about HD Radio.

HD Audio is of no real use for the simple reason that humans can’t hear any frequencies above 20 kHz. CD quality audio is the best quality audio that humans can appreciate. Who cares if you have 192 kHZ sampling rate (HD Audio) as opposed to CD audio’s sampling rate of 44.1 kHz? Oh, but they all say, “You can hear the difference.” One should say “Only if there is something wrong with your ear or brain!” (This post was inspired by a short aside in a DSP lecture, but any inaccuracies here are mine).

Note: I am writing this pretty late at night..I might have made some mistakes with the exact facts :p

More info(April 3, 2007, 10:48 am) : The reason for sampling at a bit more than twice the maximum frequency is to enable correct reconstruction of the analog signal(music and other audio in this case). You can find more information on the Nyquist-Shannon Sampling Theorem here.

Simulating Pulse Amplitude Modulation(PAM) on a Mac

One can use the IT++ library: http://itpp.sourceforge.net

This code is actually cross-platform because IT++ is cross-platform.

//Chinmoy Gavini.

//Code licensed under the GPLv2

//Update: I have tested the following code. It works. Note that the binary string below must not have spaces or endlines(of course, your text editor will word-wrap the content–that is fine).

//However, the usual disclaimers in GPLv2 apply to this code.

#include <itpp/itbase.h>
#include <itpp/comm/modulator.h>

int main()
{
PAM p(4); //We want 4-PAM
vec output; //modulated thing
bvec demodded_out;
bvec input = “0100100001100101011011000

110110001101111001000000101011

10110111101110010011011000110010000100001“;

//Binary representation of Hello World!

//The following line of code simulates the transmitter end.
p.modulate_bits(input,output);

//In a realistic simulation, we would have code to add pulse shaping and noise where this comment is

//The following simulates the receiver end.
p.demodulate_bits(output,demodded_out);

if(input == demodded_out)

{

cout<<“Hello World”<<endl;

{

else

{

cout<<“Big Bad World”<<endl;

}

return 0;
}

}

Digital Pulse Amplitude Modulation(Part 1)

Note: I am organizing my notes on Telecommunications topics. I will post programming articles too, for those who look for those kinds of articles.

Update:  April 2, 2007 10:34 PM U.S. Central Time: fixed the typo from 2-PAM to 4-PAM

Digital modulation allows us transfer sequences of bits(digital signal) over analog media(like air). Pulse Amplitude Modulation(PAM) consists of mapping a certain number of bits into symbols. For example in 4-PAM(which is named that way because a pair of bits are mapped onto a symbol), if d denotes some scalar value(like 2?), a mapping table might be:

00 -> 3d
01 -> d
11 -> -d
10 -> -3d

where 3d, d, and so on are the amplitudes of a pulse shape we choose(more on choosing the pulse shape later). For now, if we choose a rectangle wave(even though it can never be implemented exactly in a real system), which can be defined rect(t) = 0 if |t| > \frac{1}{2} and rect(t) = 1 if t < \frac{1}{2}, and rect(t) = \frac{1}{2} if t = \frac{1}{2}.

The baud rate is just another name for the symbol rate. So, if we have a symbol rate of 36 baud (36 symbols/second), in the 4-PAM scheme above, our bit rate is 72 bits/sec(bps)

Also, the mapping above from bits to symbols is optimal because it is based on the Grey code, which is an ordering of groups of bits such that group differs from the consecutive by just one bit(this is cyclical because 10 is just different in one bit from 00). Using the 2-bit Grey code helps the receiver to perform some error correction(because consecutive levels such as -d and -3d “are only 1 bit away”

References on technical info: Dr. Brian L. Evans’ lectures(Google: Brian L. Evans or Digital Pulse Amplitude Modulation(Dr. Evans’ notes should be the first hit for Digital Pulse Amplitude Modulation), Wikipedia: Grey Code

Check out the comments for the “My Favorite Software Tools Post” for more info on embedding \LaTeX in WordPress posts(info from Mr.Ali’s blog)

Examples:

$%latex \int_{-\infty}^{\infty}f(x) dx = 0$ produces \int_{-\infty}^{\infty}f(x) dx = 0

(You shouldn’t put the % in when you do this yourself)