Lost among notes

Recording from vinyl with Inspire and Cubase

September 3, 2009 · Leave a Comment

  1. Connect the Inspire to the computer and the phono pre-amp
  2. Start the Inspire software and make sure channels 3-4 are unmuted and the volume is OK
  3. Start Cubase, do File => New Project, and Project => Add Track => Audio
  4. Go to Devices => Device Setup
  5. In the VST Audio System, choose the Inspire and hit OK
  6. Go to Devices => VST Connections
  7. In the Input tab, choose channels 3 and 4 for input
  8. In the Output tab, choose channels 1 and 2 for output (connect your headphones to the Inspire, not the computer)

You will need to keep the Inspire connected at all times, even if you’re exporting a pre-recorded track to AIFF!
With this you should be able to start recording.

→ Leave a CommentCategories: Uncategorized

Xmonad

August 25, 2009 · Leave a Comment

I like Xmonad much better than any other window manager (on any platform), but the default mod key is Alt, which interferes with Emacs and Conkeror.

Here is how to remap the mod key in Xmonad

Or, locally copied on Aug 09:

Binding to the mod4 (often ‘Win’) key. Put this in yout xmonad.hs:

import XMonad

main = xmonad defaultConfig
{
modMask = mod4Mask
}

→ Leave a CommentCategories: Uncategorized

Scalar product and direction cosines

December 8, 2008 · 1 Comment

When I learned about the scalar product in high school, I was somewhat surprised by how lucky its properties are.
The treatment is usually this: define  x \cdot y = |x| |y| \cos \theta , where \theta is the angle between x and y . Then deduce that x \cdot y is linear on the left and right arguments. Then, deduce that (x_1 \vec i + x_2 \vec j + x_3 \vec k) \cdot (y_1 \vec i + y_2 \vec j + y_3 \vec k)  = x_1 y_1 + x_2 y_2 + x_3 y_3

Now, define the direction cosines of a vector x as the cosines of the angles the vector forms with the basis vectors. By definition: x = |x| (\cos \phi_1, \cos \phi_2, \cos \phi_3) .
If we define \theta as the angle between x and y , \alpha_i as the angles between x and the axis, and \beta_i as the angles between y and the axis, we have: \cos \theta = \sum_i \cos \alpha_i \cos \beta_i .

This last formula mystified me. The deduction was so purely algebraic that I found it unsatisfactory. I wanted to see the geometric meaning.

A more geometric deduction follows: (\cos \alpha_1, \cos \alpha_2, \cos \alpha_3) is a point a on the unit sphere. If we take another point b on the unit sphere, the subtend an angle \theta :
On the unit sphere

On the unit sphere, we can find the angle \theta subtended by a and b , by calculating the distance l between them:
Calculating the angle

As you can see in the picture, l = 2 \sin \frac{\theta}{2} . Therefore: 2 \sin \frac{\theta}{2} = \sqrt (\sum (\cos \alpha_i - \cos \beta_i)^2
By basic trigonometry: \sin \frac{\theta}{2} = \sqrt \frac{1 - \cos \theta}{2}

Putting the above two together: 4 \frac{1 - \cos \theta}{2} = \sum (\cos \alpha_i - \cos \beta_i)^2  = \sum (\cos^2 \alpha_i - 2 \cos \alpha_i \cos \beta_i + \cos^2 \beta_i)
Since a and b are both on the unit sphere: 4 \frac{1 - \cos \theta}{2} = 2 - \sum 2 \cos \alpha_i \cos \beta_i ,
and \cos \theta = \sum \cos \alpha_i \cos \beta_i

In this deduction of \cos \theta , we didn’t assume any knowledge of the scalar product. In fact, we could have used it as a basis for the definition of the scalar product.

→ 1 CommentCategories: vectors

Number of trailing zeros in n factorial

November 17, 2008 · 5 Comments

I was asked this question in my first job interview in this country. I was completely unprepared for this type of interview, and I did badly. But it’s a nice problem to think about and it’s easy to solve.

In case you don’t know, n factorial is the multiplication of all positive integers less than or equal to n. By convention, 0 factorial is taken as 1. The factorial is typically represented with “!’.

So:

0! = 1
1! = 1
2! = 2 1 = 2
3! = 3 2 1 = 6

And so on. You may have noted the following pattern: n! = n (n-1)! This leads to the following recursive function in the Scheme language (you can get Scheme system here)

(define (factorial n)
  (if (= n 0)
      1
      (* n (factorial (- n 1)))))

You can feed this to the Scheme system, and start getting numbers.

(factorial 0)  --> 1
(factorial 3)  --> 6
(factorial 32) --> 263130836933693530167218012160000000

So, we want to be able to compute that 32! has 7 trailing zeros, but we don’t want to have to compute 32!

The key observation is this: the number of zeros equals the number of times 10 is a factor in 32! (or n!). 10 = 2 5, and since 2 appears more often than 5 in n!, we can say that the number of trailing zeros is the same as the number of times 5 is a factor in n!

Now, how do we count the number of times 5 is a factor? We can begin by observing that 32! = 32 31 30 .. 25 .. 20 .. 15 .. 10 .. 5 .. 1 (the .. stand for omitted numbers). So, we can see 6 of the factors in 32! are multiples of 5. And we can compute this number simply by diving by 5: 32 / 5 = 6 (assuming we discard the decimal part). That’s a start.

Now, we also have 25 in there, and it has two factors of 5; we need to account for the extra five. So, there are 6 + 1 = 7 factors of 5 in 32!, and 7 trailing zeros, which you can verify above. Nice. We could have started the opposite way, counting the number of “double fives” first, multiplying them by 2, then adding the “single fives”. But if we did that, we would haveĀ  to throw away the 25 when progressing toward the single fives, and that would be more baggage to keep track of. This way, also, we count for the “double five” only once, because 25 had been counted once as a “single five”.

So, number of fives in 32! = 32 / 5 + 32 / 25 = 6 + 1 = 7

We can formulate an algorithm informally: Number of fives in n! = n / 5 + n / 25 + n / 125 … (/ is assumed to be integer division ie. it discards decimals). Let’s give this function a name: fives-in-factorial(n). By the way, the ‘…’ means ‘and so on’, but it would not make sense to go on forever, once one of the summands reaches 0.

Maybe you can see a pattern here: fives-in-factorial(n) = n / 5 + (n / 5) / 5 + (n / (5 * 5)) / 5 …

This suggests a recursive definition:

fives-in-factorial(n) = n/5 + fives-in-factorial(n/5)

The base of the recursion is that for n < 5, there are no fives in n!, hence fives-in-factorial(n) = 0

Translated into code:

(define (fives-in-factorial n)
  (if (< n 5)
      0
      (+ (floor (/ n 5))
         (fives-in-factorial (/ n 5)))))

(In case you’re wondering about “floor”: given a number, it discards its decimal part). You can again introduce this code in a Scheme system and start getting some answers. We already know 32.

(factorial 32)           --> 263130836933693530167218012160000000
(fives-in-factorial 32)  --> 7

(factorial 6)            --> 720
(fives-in-factorial 6)   --> 1

(factorial 46)           --> 5502622159812088949850305428800254892961651752960000000000
(fives-in-factorial 46)  --> 10

→ 5 CommentsCategories: code