Sunday 27 February 2011

Intervals


New blog, fresh new posts and this is my first about music. I was looking some old stuff I wrote when I was on my early 20s – at that time I was really engaged with my music studies – and I think in this first post it would be good to write about one of the most fundamental elements in music : Intervals.

As the only instrument I play is the electric / acoustic guitar, I will have the tendency to use this instrument for some examples, but I believe this post can be used by any kind of instrument.

When we are talking about intervals we are always considering two notes. When we play one note (any) followed by a second note, we get a characteristic sound. This sound results from the difference between these two notes. That difference can be measured. Like we can measure distance in meters or feet, in music the measurement unit is called Interval.

The minimum distance between two notes is half a tone. In the guitar half tone is represented by one fret. Two frets are equivalent to one whole tone. So if we take the note C, play it followed by E, for instance, what we get is a ascending distance of two whole tones and a descending distance of four tones.

However the intervals are not named as “2 and a half tone interval”, “three whole tone interval” and so on. More important than know “how far” is one note from the other is to know how does it sound!
Lets talk about sensation, feeling. If we play the notes C followed by E we have a characteristic sound. If we play D followed by F#, we get the same interval and the same sound, I mean, the same feeling, the only difference is that it will have a slightly higher register.

Let’s give name to our intervals now. Every Interval (forget the distance and keep in mind the sound they produce) has a “name” and that name represents a typical sound, independently of the key we are playing. To know the interval we will always use the first note as the reference. Also the first note is considered to “calculate” the interval. Once again taking as example the notes C and E, the interval they produce is major third – if we start counting whole tones from C to E we get: C – D – E, three notes.

The interval between D and F#: D –E – F#: 3 notes, major third. Now the interval between E and G we get: From E to F# one whole tone but from F# to G half tone. In this case we still have an interval of third, but minor third. Between the notes G and A we have on tone, this characterizes the interval of major second.

There’s a table that can help memorizing then. But again one very important thing about intervals, is the sound of each one. We can find “relaxing intervals” like Perfect fifth, Perfect fourth (try to play the sequence C – G (5th) – C – F (4th) – C – E (3th). Sad intervals like Minor sixth: try the sequence C – G# (minor 6th) – C – G (5th). And also more tense intervals like Augmented Fourth (C – F#).


Before finish I just want to point out that this material doesn't follow music theory in all of its formalities. It also may be a bit imprecise in some definitions, so consider it just as a start point.

Sunday 20 February 2011

Pointers, it was once...

In these last 10 years working as a developer or teaching in a Introduction to Programming discipline, I've found many people struggling to understand pointers properly. Many found it a very abstract subject but the truth is it is not all that difficult to grasp.

I'll try to remember as many doubts I had listened to in order to try to write about then clearly.

I think first it worth have a look again in how variables work (in a more illustrative and informal way). First a variable has a type, which determines how the bytes stored in that variable will be interpreted. The type also determines the amount of memory necessary for that variable.

Let's say we want to declare an integer variable (in C):

int x;
This variable (assuming it is an int 32 bits) will use 4 bytes of memory; this means that it can store up to 232 possible values (not all of then at the same time obviously). And what about the memory, how is it being used by the variable x? Let's think of the main memory as a huge array (once again I want to be informal) divided in blocks of 8 bits - 1 byte. As an array, each byte can be accessed by an index, actually each byte in that 'array' has an unique index called memory address through which we can access the contents stored in that piece of memory.

Let's imagine that 
the memory address 0x100 has been reserved for the variable x  - that piece of memory would look like this:



As said before, the variable x, as a 32 bits integer, is using 4 bytes starting at the address 0x100, which we say is the address of that variable. So, knowing the start address and the type, we can 'visualize' the memory space used by that variable and verify that it uses four consecutive bytes starting at 0x100.

If we assign a value to x, say 27 (0x1B in hex), the memory space would look like this:

x = 27;


The LSB (Last significant Byte) is in the smallest address of the four bytes being used by the variable x. This is due to the fact I'm considering a Little-endian architecture in this example - used by popular PCs nowadays.

Now that we have a basic idea about variables, let's talk about pointers. In some extent, pointers are like normal variables. They have a type, a name and need memory space to store the data assigned to it.

But this starts to change a little bit when we look on what type of information does a pointer variable stores. Basically a pointer stores a memory address. So in part we can think on it as a index variable (remember that we can think of the memory as a big array). This memory address can be the address of dynamically allocated memory, of a variable and even the address of a function - which we can talk about later in another post.

In a 32 bits architecture an memory address is a number that uses 32 bits of space, so a pointer will use 4 bytes. Let's go to a simple example. Considering our x variable - whose address is 0x100 - lets assign its address to a pointer

int *p = &x;
Before going any further, lets briefly talk about the symbols being used here. When declaring a pointer variable in C or C++, what distinguishes such a variable from a normal one is the asterisk (*) symbol put on the left of the variable's name. The ampersand symbol (&) when put on the left of a variable is used to get the address of that variable. So in this single line we are declaring a integer pointer called p and assign to in the address of the variable x.




In our picture now, what we have is a int variable x, whose address is 0x100, and the int pointer p pointing to the same memory address. Now, as the pointer 'knows' the address where the variable x stores its data, we can manipulate the data indirectly using the pointer. For instance consider the line bellow:
*p = 30;

What happens is that the variable x no longer has the value 27, instead we change its contents to 30 using the pointer.

Before ending this very basic discussion about pointer, a little bit of C, C++ symbology.

int *p
Here the * symbol is being used to say the p is a pointer (int pointer)

p = 30;
A pointer always interpret the data assigned to it as a memory address. So in the case we are telling the pointer to point to the address 30 - which is likely to cause problems and seems wrong as we are not sure what is being store there.

p = &x;
Now we are assigning the address (&) of x into p - note that p without the *, as written above, everything we assign to a pointer will be treated as a memory address.

*p = 20;
Now look the asterisk! It tells the pointer to assign 20 into the memory being pointed by it, we are not telling the pointer to point to the address 20.

So the asterisk when used in a variable declaration, tells the compiler that it is a pointer. However when used in the middle of the program it tells to the pointer: assign this value in the memory being pointed by you!

I think it is enough by now. More coming soon...




Wednesday 16 February 2011

My first new blog

So today I finally decided to expend some time writing down about things that I like. The aim is also to, writing those things, help me remember when I forget then. Things here can be some thoughts, concepts and theories etc... basically around topics like computer science, math and music.

The name came from two things I do and like most: I'm software engineer writing my programs in C#. C# in music is also the note C half tone higher. Music, and more specifically guitar, is the other thing I have been doing for, more or less 17 years. And just to play a bit with these words, in music D flat, which is the D note half tone bellow, produces the same sound as C#, they are enharmonically equivalent.

Using logarithms to solve fractional exponents


Other day I was studying logarithms in a basic level math course that revisited almost everything we were supposed to learn at high school when I was shocked to see how one of its properties could be easily applied to solve fractional exponents. I confess that I felt thrilled when I saw it.

Basically the property used to solve it is the one that states that:

log282 = 2.log28
Just to check this equality, the solution of both sides give us:

 log282 = log264 = 6
to obtain 64, 2 must be raised to the exponent 6

2.log28 = 2.3 = 6
So in both cases we get the same result - the property is true!

Now what about this:

y = 251/2 ?
We know this is equivalent to the square root of 25, but lets solve it using logarithms

The equality signal (=) was originally a representation of a balance scale. Both sides have to have the same "weight" to keep the balance. So, whatever we do in one side, we also do in the other.
log y = log 251/2
Applying the property we get:
log y = 1/2 log 25
Let's now define the base of the logarithm. In this case, base 5 seems quite convenient:
(It is important to remember that the base we choose must be the same used by all logs in our equation)
log5 y = 1/2 log5 25 
log5 y = 1/2 . 2
log5 y = 1
y = 51 
y = 5
Now lets consider a more complete example using almost all properties of logarithms - which can be checked out here
Adding the log in both sides we get:
Now using the properties - when a number is divided we subtract the log of both and when a number is multiplied we add - our equation gets the form:
log h = log (81)1/3 + log (27)1/2.5 - log (243)1/10
It is clear from the example that the numbers 81, 27 and 243 are all power of 3, so it will be chosen as the base.
log3 h = log3 (81)1/3 + log3 (27)1/2.5 - log3 (243)1/10
log3 h = 1/3 log3 (81) + 1/2.5 log3 (27) - 1/10 log3 (243)
Reckoning the logs at the right side we get


log3 h = 1/3 4 +  1/2.5 3 - 1/10 5
log3 h = 1.33 + 1.2 - 0.5
log3 ˜ 2

Reckoning the inverse of the logarithm, the exponentiation we get the final result - approximate

h ˜ 32
h ˜ 9


VoilĂ , logarithms are really a powerful tool, thanks to John Napier!