Category Archives: Uncategorized

Designing microcontroller project

Few reminders when designing an uC-project:

Inputs

General on inputs:

  • Primarily use first all external interrupt and analog pins. Make pads for unused “special” pins, for example external input/analog-pins.

Pulse measuring:

  • Do you need to measure pulses? Use external interrupts whenever possible, they are simple to do and really accurate

Outputs:

  • If generating pulse out by delay remember that interrupts make the signal jitter.

sed

To replace content of file in unix / linux, use sed.

Basic use, first try outputting to terminal:

sed s/blue/black/ file.txt

you can then do either

sed s/blue/black/ file.txt > replaced.txt
or
sed -i s/blue/black/ file.txt

Last command makes changes directly to file.

If you need to use non-alphabetic-characters, use \ – char in front of them. For example:

sed s/file\.txt/nonfile\.txt/ filetoreplace

More, much more : http://www.grymoire.com/Unix/Sed.html#uh-8

Goodbye Spotify

After almost two years of paying premium account I’ve decided to detach from Spotify and start buying music in CD-format.

Why?

Spotify updates itself without asking user permission. There is no way to even turn it off from the application. Why it bothers me? It’s a serious security risk. Spotify can plant any code on my machine. Probably they won’t plant any harmless functionality but think about if someone breaks into Spotify’s update system? Too easy to distribute any kind of malicious code.

Spotify adds cluttering features and redesigns UI too often. In latest update Spotify was one click away from sending my local mp3’s to Spotify. Actually based on my first rant it could have done it automatically based on their attitude and respect on users.

Spotify doesn’t pay artist enough. Yeah, I know what you’re saying: artist can decide if they want to publish music on spotify or not. I wish it was that simple.

Spotify redirects all paying customers to ask in getsatisfaction.com but fails to answer to questions. I’m paying 9,99€ per month and I wish they would have bothered to answer to my concern there but no. I’m paying customer and I think I can demand customer service. But nada. And I’m not the only one, go ahead and look.

So thanks but no thanks. I try to stick to owning the music I want to listen and not walk in a leash of Spotify and follow their rules. It’s my money.

Howto: Safari cookies – Session only

If you want to block Safari from remembering your cookies (and you should if you care about your privacy) you can force Safari from writing cookies to file with following steps:

1) Quit Safari
2) Remove the Cookies-file from /Users/[yourusername]/Library/Cookies. The file is called Cookies.plist
3) Open Safari and visit some random page that uses Cookies
4) Now you should have the Cookies.plist file again in the folder
5) Open the Cookies.plist in Finder “Get Info” and choose “Locked”

OS X application tips

Struggling with applications that open certain extension files? For example opening mp3-files causes iTunes to open them default and copy the files to it’s own library.. Get RCDefaultApp http://www.rubicode.com/Software/RCDefaultApp/

Fast, simple mp3-player: Cog http://cogx.org/

LineIn is a simple application for OS X to enable the soft playthru of audio from input devices. In simpler terms, you can use LineIn to play sound coming in through a microphone or any other device plugged in to your Sound In port, just as was once available with OS 9’s “Play input through output device” option. http://rogueamoeba.com/freebies/

Text editor Smultron http://en.wikipedia.org/wiki/Smultron

Easy and simple DVD/CD burner http://burn-osx.sourceforge.net/Pages/English/home.html

Remember that OS X internal Preview can do lot’s of stuff for images, no need to find any application for basic image manipulation.

FFT aka Fast Fourier Transform explained well

From here: http://www.kvraudio.com/forum/viewtopic.php?p=989000&sid=6e7e5d21145a3efd03b8ebaa5a15f830#989000

The fourier transform converts a time representation ( samples ) into a frequency representation ( i.e. the frequency spectrum of these samples ) and vice versa.

Commonly it splits a number of audio samples, say 2048, into a corresponding number of sine waves of different frequencies, so that you know the volumes and the phase for each sine.

The principle is very simple:

If you multiply a set of samples with another set of reference samples and add each result, like this:

result = sample[ 0 ] * reference[ 0 ] + sample[ 1 ] * reference[ 1 ] + … + sample[ n ] * reference[ n ]

… the result is an indicator for the similarity of both sets of samples.

And now with sines

Now, if you want to know the volume and the phase of a sine at a certain frequency inside an audio sample, you have to calculate 2 of these similarity values:

FC = 2 * PI * frequency / samplerate; // “frequency constant”

result_i = sample[ 0 ] * sin( FC * 0.f ) + sample[ 1 ] * sin( FC * 1.f )+ … + sample[ n ] * sin( FC * n.f )

result_r = sample[ 0 ] * cos( FC * 0.f ) + sample[ 1 ] * cos( FC * 1.f )+ … + sample[ n ] * cos( FC * n.f )

To get the actual phase and magnitude, you use simple pythagoras to get length and trigonometry to get the angle:

magnitude = sqrt( result_i * result_i + result_r * result_r);

phase = atan2( result_i, result_r) // I think…

This way you can determine how much of a frequency is present in an audio file etc., and which phase it has relative to the beginning.

Complex Numbers?

In Fourier Transform lingo, the sine part is oftenly called “imaginary” while the cosine part is called “rational”. They pretend to be complex numbers, but that’s more or less only a convention. The phase/magnitude representation IMHO feels more like complex numbers.

Getting the whole spectrum

Now, the idea behind fourier transforms is, if you start with a “fundamental” frequency of exactly 1 cycle over the whole length of the analysed set of samples (call it n samples) and do this with every multiple up to n/2 of the fundamental, you get a representation that thoroughly reflects the spectrum of the original sample. – If you transform it back (create a new bunch of n samples out of adding sines and cosines), it will perfectly fit the original samples!

Each “frequency slot” or multiple of the fundamental (1, 2, 3, …, n/2) is called a “bin”. (Note, there’s also a zero-bin, I’ll explain that later)

Fast Fourier Transform – FFT

This would be a very time consuming technique, if there wasn’t the Fast Fourier Transform, or FFT. The FFT is a simple, yet highly difficult to understand algorithm. It takes advantage of the fact, that if you draw all those sines in a graph, you have many lines that cross each other at certain points. And because they cross each other pretty often, you need only 1 multiply to gather information for many results needed. Hence, what FFT does is recursively reordering (“prepacking”, “butterfly”) the samples in a tricky way so that only a fraction of calculations are needed to create the spectrum for a given range of audio samples. The drawback is, FFT requires certain lengths (or “ns”) of samples, which are a power of 2: like 16, 32, 1024, 4096 samples etc.

DC offset and Nyquist in FFTs

Typically, a FFT outputs the spectrum in sines and cosines, but it also output 2 special values: A cosine representing the DC (offset of powers above zero and below zero), which is bin 0, or the fundamental frequency * 0 and a sine representing Nyquist (because at Nyquist, there can’t be a cosine!). Hence some algorithms require N/2+1 bins, others “pack” the Nyquist bin into the sine field of the DC bin.

Hope this helps, or maybe it’s a start…

Cheers,

Wink Urs