Making Your Computer Your Slave with Linux

August 25, 2007

Mr. Slave

And learn some stuff too while you’re at it.

Or how I learned to stop fearing my PC

Linux taught me how computers and networking works through hands-on experience, I was/am constantly trying to do stuff with Linux that I have had no experience with. I knew what I had been taught in school about computers, but that often aint nothing compared to what I learn by trying to do things I had never really done before.

Linux will teach you much about computers and their capabilities, which is necessary in the real world for any EE. Embedded Linux is also the way the embedded industry is headed. Here is a simple time-line of my recent (2004), but fun Linux/computing addiction, with example projects along the way. Warning this may get lengthy:

1. Heard about Linux from fellow geek in lab, heard it was a free OS to use along with compatable free versions of Photoshop, MS Word/Office, Matlab, h/pspice, verilog simulator, firefox w/ flashplayer, etc. Check this out for more info:

linuxquestions wiki on this subject

I heard some things were much easier, some things harder, no spyware/viruses, better security, blah blah you’ve heard this all before. You should try dual booting.

2. Installed it on an old PC and used it as my primary desktop machine as dual boot. Googled any problems/errors I had such as printing, etc. Some things were a pain, but I learned some things about software and hardware/networking that I had never known before. Usually its pretty fun and valuable to fix problems you run into yourself.

3. Decided to Setup an ssh server at home (really easy) that I could access anywhere. Created a simple web page that had php scripting enabled (so you can build interactive web sites and/or control your computer from a web page). Setup an email server (easy to do, very tough to keep out spam). Learned much about setting up and traversing firewalls/routers. Learned about setting up ssh tunnels, so you could do anything from anywhere on your private LAN through ssh. Learned lots about security too.

4. Setup a full C/C++ build environment so I can compile any program I have sourcecode to from places like sourceforge.net, etc. Play around with various scripting languages like Perl, Python, especially Bash.

5. Having taken C++, learned to use libraries to build my own short simple but powerful programs that access sound card hardware and other hardware, click buttons on the screen without a mouse (from anywhere on the net), and even wrote some simple networked servers/clients that can talk to each other on a home LAN.

6. Found out that many wireless routers (like the old Linksys WRT54G) run or can run Linux (openwrt.org). Downloaded the Openwrt SDK (Software Development Kit) for building custom software for it in C. Found out that the routers also usually have 2 serial ports and 4-8 GPIO (General Purpose Input Output) for LED’s and switches. I wrote a simple program to stream audio from another PC on a LAN to the serial port on the WRT. I had a $40 wireless speaker with WPA-PSK encryption. Used two or three simple 70-80 line C programs to do it. Fun stuff.

7. Recently started putting small Linux installs on Fanless, Hard disk-drive-less (use a usb stick), computers that cost under $200. They have VGA and full audio outputs and have no moving parts. In the pro-audio world this is pretty fscking awesome b/c it will last 10 years if it is kept out of water/beer (beer is for the equipment installer, not the equipment) and at a reasonable temp.

8. I also use gnu-octave (open source version of matlab) on Linux to crunch numbers / simulate simple DSP algorithms such as Sigma-Delta Modulators, FIR/IIR filters and architectures for FPGA’s or C code, and play w/ the amazing FFT and its DCT cousins (discrete cosine transform) which has applications in audio analysis and compression.

9. I run vmware server under Linux now so that what few Windows apps I must use, I can use them from a virtual machine (simulated computer on a computer like Parallels for the Mac) in Linux. I no longer dual-boot the two OS’s. VMWare can access USB, serial/parallel ports, and sound card access. Can’t kill windows off my PC entirely b/c unfortunately in the pro-audio world, much of the pro audio equipment is MS Windows only configurable, which is sad, but thats what happens when there is a government dependent/sponsored monopoly in a particular industry, they become “defacto standards” whether they are actually better or not. The linux kernel is capable of running uninterrupted for years at a time, and some machines have been doing so. It is very robust.

Also the command prompt or “shell” (Bash) in Linux/Unix is very, very powerful and eye-opening. It is basically a programming/scripting interface to programs. You can do things with it that will blow your mind once you learn your way around. You can use programs that they were never designed for specifically, unlike GUI programs in windows.

Example:
I have done things like encode audio from the soundcard (mic input), as an mp3, streamed it to another computer over a network and played it out the sound at the remote location (home), all with general purpose applications with one long command (mp3 encoder, ssh, mp3 decode to sound card at home). Apps in the *nix world have “standard input” and “options” and “standard output” among other interfaces and can be “connected” together with pipes and manipulated in more ways than you or even the original designers imagined. GUI (Windows) programs can’t compare. Wanna download a file without opening a browser?

file example:
$ curl http://example.com/file.mp3 > file.mp3

puts the file.mp3 in your present directory

What programs are listening on which ports:

$ sudo lsof -nPi

Find all files recursively in a directory with the string “gtk.h” and print the name of the file and line it is on:

$ grep -rn "gtk\.h" *

Linux is for people who want to know their computer better. It is a different approach to computing. It will help you eliminate your fear of the Computer, and even make it your slave. I use Ubuntu now, but almost any distribution gets you going fast (CentOS, PCLinux, Suse, Fedora, etc). Avoid slackware or gentoo unless you are really gung-ho about spending hours reading a manual, installing and getting a usable system. I know that statement will get me death threats, but I believe they have their place, and its not with a newbie unless they have help. If the newbie wants to learn the hard stuff all at once, they can use gentoo or slack, but I still prefer the simplicity of an ubuntu install and maintenence.

This is a living document and more will be added later.

Advertisements

Sigma-Delta Modulation Primer Part II

June 30, 2007

UPDATE! Code now bypasses so16() function which would have caused errors for you.

Well here is the first order Sigma-Delta Modulation code for matlab or octave . You will need to download the wav file, or use one of your own that has audio near 16kHz sample rate, and has audio between the first 2000 and 10000 samples. I cannot currently post wav files because wordpress will not let me. If you don’t want to mess with wav’s then you can use the square wave version.

Click For Larger View:
Sigma-Delta Modulation

Go to: http://www.dailywav.com/0502.php Right click on the Space Ghost clip (whatsup.wav) file and do a save as to the same directory as the code. I would provide a direct link but they don’t seem to allow hotlinking.

Here is the first order SDM code for a wav file (GNU Octave):

Created with Vim 7.0 command :TOhtml
%    firstOrderDSM.m
%    Implements a first order Sigma-Delta Modulator
%    Copyright 2007 Brian R Phelps
%    This program is free software; you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation; either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see http://www.gnu.org/licenses/clear;

[x,fs,fmt]=auload('whatsup.wav');
x=x(2000:10000);
NumSamps=2000;
OSR=10;         % The over sampling rate
NumSamps=length(x)
z1km1q = 0; % Initialize variables
z1km1 = 0;
% Scale the data to 16 bit "integers", hardware 
% in real life is integer or fixed point math
%returns scaled integers
x=round(x*32766);
for n=1:NumSamps
   z1(1)=z1km1;
   xn=x(n);
   for k=1:OSR  % Each sample is Oversampled OSR times
      % please see the diagram for an explanation for the following:
      z1(k) = z1km1;
      z1km1 = z1(k) + xn  - z1km1q;
      z1km1q = (z1km1 > 0) * 32766 - (z1km1 <= 0) * 32766;
      y(k+(n-1)*OSR) = (z1km1 > 0) - (z1km1 <= 0);
   end
end

b=fir1(121,1/(OSR*2));  % A low pass filter is also an integrator (summer), 
                        % either way it is neccessary to recover the original signal
y=filter(b,1,y);        % This gets rid of the noise, which 
                        % most of which is moved out of the passband
y=decimate(y,OSR);      % Keep only 1/10 samples to get the 
                        % sample rate back down to original
plot(1:length(y),y );
sound(y/3,fs);

Sigma-Delta Modulation Primer Part I

June 24, 2007

Also known as Delta-Sigma modulation. Ever wonder how most sound cards and DACs and even many ADC’s work? The higher quality ones use Sigma Delta Modulation. The theory behind this algorithm is that you can approximate an analog waveform using just a one bit output, and an integrator (Low Pass Filter). This is similar to the way PWM works, but is actually capable of much better resolution for a given set of hardware constraints. Higher order SDM’s require multiplication and addition, which makes the hardware more expensive than a simple PWM. See Figure1 for a low order SDM:

A 1st order noise shaper

This is a first order DSM. It uses an integrator to do noise-shaping and feedback to approximate the input. Basically it tries to match the output y[n] with the input x[n] by oversampling. The higher the oversampling rate, the higher the Signal to Noise ratio (SNR). A typical 4th order DSM can achieve nearly 90-100dB SNR if the oversampling rate (OSR) is around 45. So for 44100 kHz audio we would oversample at 1984500 Hz to achieve this SNR. Keep in mind the y[n] must be fed into a low pass filter.

The SDM has two interesting characteristics that make it useful for audio:

1. It has a dynamic range that is basically only limited by OSR.

2. It works on Digital PCM (sampled) streams or analog data.

Think of it as a guesser that can either output y[n] as a +1 or -1. The +1 or -1 is fed into an integrator many times faster than samples are arriving depending on the OSR. The integrator can be thought of as a summing device, in reality it is a low pass filter. Say your input sample is a 2 and the OSR is 5.

(Previous Value)+1+1-1+1-1+1=2

The next sample is a 1 so:

(PV = 2)-1+1-1+1-1=1

I also used used a friends account at a university to model a 4th order SDM with an OSR of 45. Octave does not yet model SDM’s, but it obviously simulates them if you write the code. Modeling Sigma-Delta Modulators is difficult and involves some control system theory. Basically higher order SDM’s run into stability issues. Again, modeling is more complicated than simulation and implementation, which is what these articles focus on

I have some matlab/octave code that does a 1st order SDM on a wav file. I will post some of my code later in another post. I will post the block diagram for this soon on a follow up. The 4th order model and code/coefficients will also be included.


ARM-based Open Embedded Linux Platform

June 24, 2007

Check out the Linuxstamp over at opencircuits. This is a great project that aims to make a simple (I hope they keep it simple), small, inexpensive ARM (with MMU!) platform that can run on POE. I can’t wait to build one. It looks like a fun project.

screenshot-2.png

Here are the features:

  • Atmel AT91RM9200 processor (Arm9 processor with MMU)
  • 32MB SDRAM (Only limited by 1x 54-TSOP SDRAM chip)
  • 8MB SPI Dataflash
  • 1x 10/100 Ethernet
  • 1x USB host port (allows wifi adapters, flash drives and other USB devices to be used)
  • 1x SD card slot
  • Serial debug port access through FTDI USB/Serial converter
  • JTAG port
  • 2-Layer PCB design
  • POE capable (48v -> 5v Power supply can be implemented on a motherboard)

They have posted the Eagle CAD files and the BOM. I really am happy to run across a design like this.

Most ARM platforms capable of running embedded Linux are fairly expensive and have lots of extra useless garbage ($150 or more). This one is supposed to be like $75 including the board. It is using a 2-layer board, but it might be OK if the groundplane is respected well. They seem to be confident it will be OK. I am a bit worried about how the RAM/Flash buses will act up with no enclosed planes, but my experience in highspeed PCB design is nonexistent, but I do try to understand the theory behind it. It will likely be OK with the traces kept short.

I am very interested in seeing this design work. Its difficult to design a computer motherboard from scratch because there is just a ton of supporting hardware and connections. I imagine it will be fun to try to get it to boot. Good thing there’s JTAG.

Audio Engineering With Linux

June 24, 2007

Update!

This project will use the alsasound (audio), libgoffice which is used by gnumeric (plots), and the fftw (fast-fourier transform analysis). This project will use these three libraries and gtk (for the gui). There are many different ways to perform this task, I chose these packages because of my experience with all of them. I am using the M-Audio Mobile Pre USB for audio I/O. Laptop soundcards stunk and typically only have one input, but if used properly, it may be possible to do this project with one input.

SM58

I am a Linux user. I use Ubuntu 6.10. I have tried other Linux distros and I currently run several. But my main laptop, is Ubuntu. This is a problem/challenge to the real world and the workplace, as most software is written for MS Windows. An example is <an unnamed pricey proprietary software package>, an audio diagnostics tool.

I usually use this pricey proprietary software package to calibrate professional sound systems using its “Transfer Function” feature (output over input). It works very well and is quick to perform, but very difficult to set up and actually perform accurately. The basic idea is that I need to be able to view the output of my sound card divided by the input to it, in the frequency domain usually done using the FFT. I have written a program that takes samples from the mic input, and send data to the line out. I have also used the fftw library to analyze samples. I have even written a few small GTK (a nice gui toolkit) programs. I think I can pull it off, but it will take some time. I am an old school C programmer. I have used many languages such as PHP, Python, Perl, but I always come back to C.

What I am going to do is write a simple GTK based program that allows me to perform the transfer function on a system. It will need to take several samples at different room locations and average them together so that I can use them to calibrate the system. It will need to be able to time-align the frequency data (delay) so that we end up with accurate curves.

Assuming I am not violating any software patents, I will release the project under the GPL on sourceforge. I have looked for a similar open-source project but it doesn’t seem to exist yet.

As I always do for any design, I will Sketch it out Before Coding™. This helps me avoid a complete rewrite midway through the coding.