Friday, 6 November 2009

Physics modelling of string instruments

It's been a while since my last post, but much real life stuff has been going on in the last year, and I haven't had the opportunity to do much programming.

Anyway onto todays topic .. after having spent a bit of time working on my sequencer / sampling / composing program, I thought it would be fun to have a go at little programs for synthesizing instrument sounds, either to import the results as samples into the composer program, or to develop a plugin architecture for instruments, similar to the steinberg VST instrument approach (but on a more basic level).

So it occurred to me that by using some simple physics models, it might be possible to get some interesting sounds from the complexity that often occurs in physics simulations. Obvious candidates seem to be a string (such as a guitar or piano) and a drum skin, the drum skin being a 2 dimensional version of the 1d string simulation.

I have a very vague idea of how the string system works, depending on the length of the string you can get a base oscillation with the wavelength of the string, then various overtones where multiple oscillations can fit into the length of the string.

After a quick google it (not surprisingly) seems several people have used the same approach, I found an interesting thesis on the subject by Balazs Bank, with some very impressive piano simulations, which got me interested!

Anyway I have had a quick go at doing a very simple simulation of a 1d string, then exporting the results as a wav file.

Each element of the string has a 1d position (amplitude) and 1d velocity, with the velocity altered according to the distance to each of the neighbouring elements (i.e. a lower neighbour pulls the element down, and vice versa). This velocity is used to determine changes to the position, and finally damping is applied to the velocity each iteration. Each end element of the string is clamped to 0.0 position and 0.0 velocity. A very simple model really, but enough to get some waveforms happening!

I really want to get things happening with a decent length string (hundreds of elements) but for now I've just been trying small numbers .. presumably the results depend on the parameters used for applying velocity, damping etc.

So here's some results:

3 elements (1 active element, as the ends of the string are clamped to 0.0)
5 elements
7 elements
21 elements

So far for 'listening' to the result I had just been outputting the amplitude of the middle element of the string. Instead I decided to take a slightly more realistic approach and sum the amplitudes of each element of the string:

21 elements (summed amplitudes)

This muffled off a lot of the higher frequencies and made a bit more of a realistic sound. Presumably in a real instrument the way different frequencies are muffled off or amplified can depend on things such as the shape and resonant frequencies of the box around the string (the wood around a violin for example).

Not really any super usable sounds yet, but quite promising for an hour or so's coding.

Thursday, 16 October 2008

How would I solve the global financial crisis?

I suppose everyone has their own ideas on this, and I'm not an economist, I'm probably being very naive but I thought I'd have a go.

On holiday I was reading plato's 'republic'. In here he describes some kind of 'ideal' communist society, where everyone is working for the common good, like worker drones in an ant colony. Lots of staples of communist regimes like censorship, indoctrination etc. Not really my idea of an ideal society. But it does make you think.

Personally I believe that there is a balance to be made between pure capitalism and free markets (as the USA strives towards) and pure socialism (where everything is done for the state, and their is no reward for enterprise). I am not sure where the exact best balance lies, but there are some clues. I saw the movie 'sicko' recently which also examines one aspect of this question. It examines the reality of a free market health care system in the USA versus the more socialist systems in e.g. the uk or france. Ok it vastly overrates the usefulness of our NHS (us brits think it's not up to much), but I'd take that any day over the system they have as portrayed in the US. Medical insurance is fine in theory, but if they really can refuse your care on a technicality then the whole system becomes quite sick really (as with the movie title).

So while I believe very strongly in free markets and having reward for enterprise, I also think certain services are best provided by the state - such as health, police, fire etc.

Currently with the bank crisis I find myself asking some similar questions about the banking system. If capitalist societies rely on banks as their backbones, the foundations upon which society is laid, then the banking system must be stable as a rock. As detailed in the previous post, there are some very serious and fundamental flaws with the current banking systems. While they work very well on sunny days, they really become worse than useless on rainy days.

For something so fundamental I have to propose 2 possible solutions.

One is that the state should run the fundamental banking system for a country (or even possibly the concept of a world bank?). Nationalization - the banks money IS the states money - there can be no more confidence than that I think. And if the state takes on toxic debts from e.g. the US, then it is it's own fault, it takes the hit, the tax payers complain and vote in a more prudent government next time.

The second solution, probably more likely as less radical .. is that the banks, as 'keystones' in the economies of the world, are subject to intense regulation in everything they do. Every loan, debt is recorded and audited and viewable (perhaps even by anybody?) so that their financial books are constantly under scrutiny, much like open source software. And have independent bodies (presumably this is what the FSA is meant to be for) scrutinizing the books and imposing vast penalties for taking on too much risk.

However, as one banking expert pointed out, the problem with the second solution is, that it is easy to spot a risky loan in hindsight, much more difficult to manage risk on a day to day basis. Mind you you can't help thinking that it must be possible to manage risk better than certain institutions were doing (e.g. northern rock).

It is interesting that the banking systems used to be far more highly regulated, but I believe that in the reagan and thatcher years, many of the regulations were removed, and more so by brown and his cronies. Perhaps they were the true architects of this crisis.

On top of all this, one also has to ask some questions about the whole fractional reserve banking 'con', that exacerbates the whole system in times of crisis. The question is, could we live without it? Or has it become the engine that drives our economies?

The Credit Crunch

A while since I posted, but I've been on holiday and since I came back I've been non stop having to deal with the symptoms of the financial crisis. It's certainly made me read and understand a lot more about economics. I'm still far from knowledgeable about it all.. but here are a few thoughts on the matter.

First to set the scenario:
Over the past year there has been increasing worry in the banking sector about the prescence of 'sub prime' loans in the system. In the USA in particular, the banks had been so eager to keep making profit they had loaned money to people to buy houses, where they had no hope of ever paying off the mortgage.

Instead of keeping this debt on their own books, these banks had packaged up these debts and sold them on to other international banks, while presumably downplaying the risk involved that the debtors would default.

Of course if the debtors defaulted, there was always the house left as collateral for the loan - i.e. the banks could reclaim the house, sell it on, and get back the money that was owed.

However, an added problem is that house prices in the US (and the UK) have been dropping. In many cases also it could prove nigh impossible to sell on the houses once the house buyer defaulted. This meant that there were an awful lot of 'sub prime' loans in the international banking system that were not worth a hell of a lot.

Of course the original home loaning banks didn't really care too much - they had sold the toxic debt on. After all, there seems to have been little regulation in this industry.

Throughout the last year worries about this problem have been spreading throughout the global banking system. Banks work by taking deposits from us, then investing most of that money themselves, by mechanisms such as providing home loans, or lending it on to other banks to invest, at an inter bank lending rate (LIBOR). They keep a little cash on hand, just in case some depositors come asking for their money back, but the vast majority is out there invested somehow, making the bank interest.

Now what has happened is that banks have become worried that their lending neighbour banks may have become contaminated with lots of worthless sub prime loans on their books. As far as I can see, the books of the banks are confidential, so there seems to be games of rumours and chinese whispers as to which banks have lots of their money tied up in these worthless loans.

The problem is, that if I lend money to a bank that has lots of toxic loans, and that bank runs into liquidity problems as a result of the toxic loans, then I'm not sure if I'll get my money back. Now consider that this applies for both me as a depositor, AND other banks in the inter bank lending market.

The banks really don't want to risk lending to each other because of this high risk, and thus the interbank lending rate (LIBOR) is sky high. However that means that the only way banks can make money and operate is through their own cash from depositors, and making loans etc themselves rather than via other banks.

This results in the banks having to offer very high interest rates because they are desperate for the cash from depositors, which effectively means that in the past few years they have become less and less tied to central bank (e.g. bank of england) lending rates, i.e. they are ignoring the moves that politicians make, because free market forces have taken over.

It also means that with this small amount of working capital the banks have to be VERY careful about who they loan it to (to make profit), AND they will only loan it at high rates of interest, as they need to make money to survive.

This means many businesses (particularly small ones) will apply to their bank for a loan to operate, and be refused. Businesses are thus having to downsize, or go under, from lack of loan money, and thus a lot of people are going to lose their jobs. This job loss stage is just beginning. When people lose their jobs, or are worried about their jobs, they cut back their spending, thus lowering the money made by other businesses, leading to more redundancies etc. The cycle continues and we have a recession.

But wait!! It's even more complicated than that. There is an extra 'BONUS' risk. This can be quite tricky to understand, so I'll say it slowly:

Banks are basically more advanced versions of the 'money lenders' in the temples in bible stories etc. The idea is that if you are rich, you can either hold onto your current wealth, or you can make it grow even bigger by lending it out temporarily to other people, but charging them 'interest' or a percentage for the privilege of having this lending.

Of course if you are going to do this, you need some kind of mafia scenario, where you have enforcers to beat up your clients because many of them are very unreliable and will need 'persuasion' to pay back your loans.

I digress ... anyway this was the initial system rich people, lent out their money and got it back with interest. A few years later some bright sparks came up with the concept of a bank. Instead of having a rich guy provide the capital, a company (the bank) would build a big vault to ward off robbers, then offer citizens the ability to deposit their cash in the bank (to keep it safe).

The citizens were happy, they could keep their cash safe (or safer) than under their mattress, and the banks had capital, some of which they could lend out and charge interest to other businesses, home buyers etc.

The above is a simple banking system. There is however a problem even with this system. Because the bank has invested much of it's capital, if all the savers came to the bank at once and demanded their money back, they couldn't have it!! The bank would suffer a liquidity crisis (a technical way of saying they didn't have the cash) as it was tied up in loans to other people / businesses.

This scenario is called a 'run' on a bank. Providing people have confidence in their bank, then on average only a small percentage of savers will be asking for money out on any day .. matched approximately by other savers putting money in. In this way providing the bank keeps a reasonable amount of it's capital in cash form (not invested), it can stay solvent.

But wait, here's the mad bit. At some point along the line banks stopped using hard cash e.g. coins etc to lend, and started using in effect 'I owe you' notes, for lending and mortgages etc.

Then some incredibly bright spark(!) invented what is called fractional reserve banking. If a saver deposited say 100 pounds into their account at the bank, the bank would (theoretically) have 100 pounds it could then invest and lend out to e.g. a homebuyer somewhere. This is very logical.

One day the bank managers met up with themselves, and decided, they were reliable fellows - why not increase their potential to make profit, by allowing themselves to lend out MORE money than they had in deposits!! i.e. when a depositor gave them 100 pounds, this wasn't going to make them much interest on investments, so instead they would invest that 100, but also conjure up 900 from thin air, and invest that too!!

After all these notes they were issuing for mortages etc were only IOUs, they could write anything they wanted on them. And they were reliable sorts these bankers, providing everyone paid their loans back, then they would make 10x the profit, and no one would suspect a thing!!

So fractional reserve banking is kind of like a con trick, except it has become accepted as the conventional way of doing banking. That is because, in most cases, it works ... it applies 'leverage' and makes 10x the profit from the same amount of depositors money.

However this con can multiply the problems caused when a bank runs into trouble. In normal businesses, when they run into trouble, they go into administration, and the administrators split up the assets of the company that are left, sell them off and split the money left among the people that the company owes money.

But with a bank, most of the debts that the bank has, it made with IOUs, they were never backed by real money!! That means when a bank goes under, in many cases the IOUs will become almost worthless. This means that in a risky climate, banks are incredibly risky things to lend your money too.

And this is what is happening, the whole banking network is built on the fraction reserve 'con' trick, so the banks are incredibly wary of lending money to each other just in case one of them goes down. And if one goes down any banks that have lent money to it will suddenly find themselves a lot worse off, and in a position where they could go down. And then again any banks which rely on this second bank get taken down, and the cycle continues. The danger is that the whole banking system can fall over like a stack of dominos.

This is essentially as I understand it what happened in the wall street crash of 1929 and the following depression of the 1930s. Very large numbers of banks collapsed, millions of people lost their savings, and lots of businesses went under and their was massive unemployment.

The US government at the time believed strongly in the free market capitalist system 'to the end', and thus didn't provide any help when this situation occurred. It saw it as the 'weak banks' being taken out leaving only the fittest still standing.

Of course it doesn't actually always work like that. And they neglected to realise that the knock on effect of this would be a collapse of the rest of the economy, as everything in capitalism depends on the banking system, ie. banking is the backbone on which everything else lays. If the banking system goes, your whole system of society is at risk (you can end up with anarchy, everyone for themselves).

This time round the scenario is very similar. Most people have been unaware of the risks involved here, they are too busy watching 'big brother', or seeing what madonna or kylie are up to, they aren't 'interested' in financial matters. It reminds me of that scene in constantine, where keanu asks the woman 'do you believe in the devil?', 'no' she replies. 'Well he believes in you!'. It doesn't matter whether people have any interest in the financial system, they still are wholely dependent on it for practically everything in their lives.

This time round most casual observers make the same comments and mistakes that were made in the 1929 crash. 'It's the banks fault, let them go down'. Of course the stack of dominos would result, and the world could fall into the abyss. Quite frightening that these are also voters.

Luckily those making the decisions (well some of them) are a bit more versed in the hazards and the knock on effects. We stand on the edge of the precipice. As far as the governments are concerned they want to maintain the status quo. On the surface the problem is one of confidence. They want to restore confidence. Confidence on the one hand to depositors, in order to prevent runs on the banks. And confidence on the other hand to the banks so they will lend to each other, and hence make them more able to provide loans to businesses and homeowners that keep the economies of the world ticking.

The latest plan used by gordon brown, alistair darling, and now being followed to some extent in many countries, is to address these problems by providing capital (to prevent liquidity problems) and to provide guarantees to inter bank lending, to get the banks lending to each other. It is in effect trying to provide a giant band aid to the current banking structure / status quo.

Of course, because of the fractional reserve banking system, the figures involved are enormous, but hey the tax payers have no choice, they elected their governments... Besides it's just going on the countries own debts (they each seem to have made some kind of international 'tab', another con perhaps?). And the argument is that if it works, it's only a loan because it's a 'guarantee' and insurance against the bank lending, everything should work smoothly. Shouldn't it?

Well now we are beginning to see the signs. The plan was accounced around a week ago in the UK, and rather more recently in other countries. The FTSE / dow jones etc all jumped on the news of the global 'bailout' for the banks. But now it is falling again. People are beginning to realise that the problems of trust are more endemic and are being very hard to solve .. they will probably take years to return to normal levels of trust (and I doubt they will without some kind of modifications to current systems).

The inter bank lending rate (LIBOR) in the uk at least hasn't responded as gordon brown / darling would have hoped. In short they still aren't lending. We are still on the edge of precipice. And what's more, many of the governments have 'shot their load'. They don't have infinite finance. They can't carry on pumping billions and trillions to prop up the banking system indefinitely. And the bad debts of lehman brothers are going to be looked at shortly. What other institutions might go down as a result of this? What will be the knock on effects of several european banks going down, in iceland, in britain, in france, in germany etc. What were the interlinks? Will the stack of dominos start to fall?

Interesting times!

Wednesday, 30 July 2008

Knowledge Storage

I'm back at work on Egor now.

While the old version handled sentences such as 'what is a cat?', I want to now extend this fully to WH questions (what, why, where etc):

Thus questions such as 'where do you live?'. The old system was a bit of a bodge. Now, when a question such as this is formulated, it adds an entry for the 'WH-word' unknown into the knowledge tree... it can either identify the answer now or perhaps come up with the answer at a later time when it has more knowledge.

An interesting thing happens when you look at slightly more complex variants of these questions.

For instance:
------------------------------------
The cat eats sardines in the kitchen.
The cat eats mice in the garden.

Where does the cat eat sardines?
------------------------------------

Initially I was storing the information that the cat eats sardines, and the cat eats mice on separate branches (sub trees) from the subject. However, it occurred that reusing branches may be the way to go, both in terms of efficient compression of information, but also in terms of speedy and efficient access to the information.

However, once you start compressing the information, another 'issue' appears:

If you store, 'the cat eats sardines in the kitchen' in one tree, it essentially doesn't matter the order of the object and supplementary information...

i.e. the cat eats in the kitchen sardines = the cat eats sardines in the kitchen.

Once you start compressing several sentences of information in the same subtree, you then have to start considering the order of information.

Thus: The cat eats sardines in the kitchen, The cat eats tuna in the kitchen...

You may start to think of this as a hierarchy: cat -> eats -> in the kitchen -> sardines / tuna

However, this has many implications. Firstly you can no longer directly store information as generics (i.e. in tree terms the 'in the kitchen' needs to be distinct and have child nodes). This is an added level of complexity - so we would have to be sure we were getting a payback for that complexity.

In addition, once you start to consider several pieces of supplementary information for a sentence, the optimum storage arrangement may not be obvious (i.e. how are you going to regularly access this information determines the best tree structure).

As I am modelling things according to how biological systems tend to work .. there is also the point that biological systems often take the simplest path (making complexity from simple rules) rather than working with a complex 'operating system'. I.e. there is a danger of anthromorphosizing the problem - producing a computer science solution instead of a simpler (possible) biological solution.

I am not sure which one to go with at the moment, because it seems a major design issue. I may well start by experimenting with the simple approach. It may turn out to be incorrect (and later need a considerable rewrite), but the fact is that the whole project is a huge undertaking and I would rather have a simple system working than a more complex system that I didn't have nearly enough time to get to a working state.

In essence I can't hope to get everything perfectly right and optimal on my first attempts, I think this is something that will be refined in many decades to come, to one or several optimal solutions.

Tuesday, 8 July 2008

Ending Poverty - Why Geldof's View is Naive

I read today how Geldof is again urging the G8 to 'help the poor' in africa.

A long time ago, do gooders in the 1st world countries noticed the poverty in third world countries, and decided that the best way they could help was by 'charity' and providing loans so these countries could supposedly get on their feet and support themselves to the same 'standards' of the first world countries.

What in fact happened was that money and aid was provided to corrupt governments, who mostly squandered it, leaving the country in debt for stupid amounts of money it had no hope of repaying, with the interest each year on the debt being too much to pay let alone the full amount. This is now generally regarded as a mistake and is referred to as the 'third world debt', and in some cases has been cancelled by the issuing countries.

Yet still there are those that believe that somehow these countries will only be able to advance if given sufficient pots of gold from the first world.



If we ignore the problem of corruption, and totally inappropriate aid (for example education in places where there is no opportunity to utilize that education), there is still an incredibly glaring reason why increasing aid is unlikely to reduce human poverty and misery.

It stems down to very basic population ecology - the concept of the 'carrying capacity'.

From wikipedia:

"The supportable population of an organism, given the food, habitat, water and other necessities available within an ecosystem is known as the ecosystem's carrying capacity for that organism. For the human population more complex variables such as sanitation and medical care are sometimes considered as part of the necessary infrastructure.

As population density increases, birth rate often decrease and death rates typically increase. The difference between the birth rate and the death rate is the "natural increase." The carrying capacity could support a positive natural increase, or could require a negative natural increase. Carrying capacity is thus the number of individuals an environment can support without significant negative impacts to the given organism and its environment. A factor that keeps population size at equilibrium is known as a regulating factor. The origins of the term lie in its use in the shipping industry to describe freight capacity, and a recent review finds the first use of the term in an 1845 report by the US Secretary of State to the Senate (Sayre, 2007).

Below carrying capacity, populations typically increase, while above, they typically decrease. Population size decreases above carrying capacity due to a range of factors depending on the species concerned, but can include insufficient space, food supply, or sunlight. The carrying capacity of an environment may vary for different species and may change over time due to a variety of factors including: food availability; water supply; environmental conditions; and living space."


In many third world countries such as africa there is a tendency for large families. That is, with no social security or pensions, a family depends on their children for survival and prosperity. Thus there is as in many species the tendency for the population to increase dramatically over time, if we reduce factors such as disease, war and malnutrition.

In the areas that cause the most concern the population is often by and large limited by these 'misery' factors, such as poverty and disease.

So you have in a village for example, 500 people leading an ok life, and 500 people living in absolute misery, on the brink of death.

Now let's have a look at what happens when you apply aid from the 1st world.

Initially there is much happiness as all of those 1000 people are released from complete poverty and can live an ok life.

However, the problem comes when you consider the population size over time. With this extra help, more of the population live to an older age, and produce many children. The aid that was once there either dries up, or best case stays at the previous level.

What you now end up with is for example, 2000 people living in the same area.

With aid removed, perhaps the land can support 500 people to live comfortably, and now 1500 are living in poverty!! Or best case you have 1500 people permanently dependent on outside financial support.

That's right, think about it for a second. By all that 'do gooding' action, you have let the population increase beyond it's carrying capacity, and you have in effect, tripled the human misery!!

This is why any 1st world intervention to supposedly 'help' a 3rd world country must be carefully planned - because you can see that in the majority of cases, it will result in an increase in suffering, rather than a decrease.

It would seem that the most obvious thing to do to decrease suffering in a harsh part of the world, is to limit the amount of children, so that those that do live there can be better supported by the environment.

Sunday, 22 June 2008

Web Browser

It's been a bit of a gap since my last post, I managed to get sidetracked to doing some work on a website I setup last year .. which involved lots of php, mysql and javascript. So now I'm back ready for some 'proper' coding I needed to do a refresher on c++, so I've decided to have a quick go at a second version of a web browser I wrote a couple of years ago.

The 'skanky sea dog' web browser is just a bit of fun really .. I'm just doing it as a little learning project so I can learn the details of how html / web servers work. The first version was very simple, it downloaded the html for a page, and did some incredibly basic processing of the html to show some text on the screen, and download some of the images (more a proof of concept).

This time, after learning a bit of css and javascript, I have a bit better understanding of how the html DOM (document object model) works, and as it is a tree structure, and I have a fair bit of experience at dealing with tree structures (they seem to crop up everywhere in coding), I thought i'd give it a go at parsing the html into a tree of c++ objects, with different types for different html tags.

The parsing was actually not all that tricky, I had earlier written an xml parser, so I modified it to get some useful functions for html parsing, then allowed the tree to 'build itself' by parsing the html - i.e. it encounters an [html] tag, it creates an html node, and begins parsing for child objects within this node. When it finally encounters the [/html] tag, it is finished with the node, and moves up the the higher node on the tree (in this case the document node), until all the document has been processed.

For rendering, I knew that I had to somehow implement a version of the html 'box model', i.e. child elements determining the size of parent elements, or parents constraining the size of child elements etc. I have no idea how firefox and IE handle this, but I have done it using the old trick of traversing the tree.

I do several passes, down the tree to the leaf nodes, then up again to the root, doing different operations on each pass, gradually refining the box models for each element (things like widths, heights, minimum width, desired width, offsets etc).

This seems to be working, and now I'm doing some refinements for tables to allow the columns and rows to line up.



For rendering, I wanted ideally to make things cross platform, but as I'm only familiar with windows, I've tried to separate things a bit for the rendering code. Each html element has it's own win32 window. Whether that's a good or bad idea I don't know yet .. I basically had the choice to draw everything manually, or rely on the win32 techniques. While I like doing everything manually, win32 normally makes it an enormous pain the ass to do anything manually, so I'll go with the flow for now and see what happens.

Incidently finding the sizes of image elements is easy .. text elements is more tricky. I used things like getting the text metrics and finding the pixels used by each word in turn to determine the minimum widths and desired widths for text elements. In fact in some elements (e.g. [div] or [body]) you can have text elements intermingled with images etc so it is slightly more complex determining widths etc but it is very doable.

I get the feeling that while the overall structure of the browser code is quite well laid out, the intricasies could quickly become a bit of a rats nest because I think there will be so many 'special cases'. This may be part of the explanation for the differences in behaviour between Internet Explorer and Firefox, however I think there is probably some fundamental difference in their box model calculation method which leads to their 'quirks'.

Monday, 21 April 2008

Egor videos

After a long gap, I've finally got back to doing some more work on Egor. It's actually been really good having a rest, and coming back with a fresh perspective.

Anyway I did a couple of videos this morning, showing some of the basics, hope you like!