Rāda ziņas ar etiķeti math. Rādīt visas ziņas
Rāda ziņas ar etiķeti math. Rādīt visas ziņas

2014-09-07

Harmonic oscillations


Mathematics which explains the distance
at time for harmonic oscillations.
Harmonic oscillations and the representation of distance, speed and acceleration over time. So, if we derive the distance over time, we get the speed. If we derive that, we get acceleration.

This distance at time is composite function which has inner and outer parts and constant. Which parts in the formula are constants? Amplitude, angular frequency and phase shift. Firstly we see that its a multiplication of constant (amplitude) and composite function (frequency, time and phase within the sine). So we leave amplitude intact and will multiply it with derivative of outer function which keeps the inner function. So, cosine becomes negative sine, and then multiply it with derivative of inner function. Inner function is the first order polynome, so phase dissapears and frequency loses time. So, the derivative of inner function is pure angular frequency. So, recall, how far we are - we multiply amplitude with negative sine of intact inner function and multiply it with derivative of inner function - angular frequency. To get rid of negative sign, we can change sine to cosine by adding pi/2 inside of  this trigonometric function. Remember that sine of angle is equal to cosine of angle plus pi/2.

2013-01-11

Telephone number representation in ... one pixel..

Lets say I have a telephone number which consists of 8 numbers - "29945987"
How to shorten it? Well.. one could convert it to different numeral system with base greater than 10.. The most popular would be hex numeral system with base-16. In that case the number would be "1C8F083" with only 7 symbols in it. My friend tried to convert it as an ASCII but didn`t do it mathematecally correctly :D so he has to rework it.

Ok, but - how to get it on a ... pixel? Well, what exactly is pixel? Working with different imagery software (starting with most popular and simplest Microsoft Paint and ending with complex Adobe Photoshop, Gimp, etc) users can "paint" or do whatever they are doing with custom made colors chosen from software color palette. The color "consists" of three "base channels" representing colors - red, green and blue each having a value from 0 to 255. So each pixel has these three values. Even better, there is some "pixel formats" with additional alpha channel for opacity. 

What does it mean? It means, that in the one pixel there can be stored value, which doesn`t exceed 255^4 =4`228`250`625 - numerical information with 9 characters is safe to save in one pixel. Even better - in two pixels we can safely save information about 19 numbers or the maximum value stored in two pixels is 1.78781033478129 × 10^19 or 17878103347812900000.

The next thing is - how to encode and decode such an information?

Lets try to encode my number "29945987" in pixel step by step. Despite maybe there are another approaches how to do it, this one is mine:

The main rule is I have to split the number in 4 parts with values from 0 to 255.

The first part will be the alpha channel.
The value of alpha channel is calculated as the floor value from division of my number and the value 255^3

floor(29945987;255^3)=floor(29945987;16581375)=1 (alpha)

The second part will calculate the value for the red channel. For that we firstly need to calculate with remainder (first) of difference between given telephone number and 255^3. 

mod(29945987;255^3)=mod(29945987;16581375)=13364612

Lets see is this number dividable with 255? Not really, there will be again some remainder (second) left. In such a case this second remainder can be any value from 0 to 255. Let us this value be the value for red channel

mod(13364612;255)=62

From here everything tangles, could be hard to follow.
What else we can do with the remainders we have? Previously we got second remainder with value 62. Subtracting it from first remainder, can this value be divided with 255^2?

13364612-62=13364550

In this case no, there is third remainder with value 34425.

mod(13364550;255^2)=mod(13364550;65025)=34425

Again, we are calculating the difference between second and third remainders getting the number which can be divided with 255^2 without remainder value=0.

13364550-34425=13330125
The division between calculated difference and 255^2 will be the value for third - green - channel. The calculated value will be lower than 255^3.

13330125/255^2=13330125/65025=205


And the last - blue - fourth channel value will be the division between third remainder and 255^1

34425/255 = 135

Does this all seems familiar? Well, this is my today`s empirical approach how to get values.
Actually - the similar approach could be done using base-x algorithm where x is any integer. In my case it should be 255.

And this is the RGBA color of my telephone number
And here it is in one small pixel:





2012-11-12

another "long term monthly mean" script


I always try to simplify and optimize things when I write my scripts. When I firstly started to write some scripts which iterated over a dates, I didnt realize the importance of splitted datetime objects by its parts and I wrote these scripts complicated with different inner conditions and so on. This one is very basic and simple one - it takes apart the "datetime" and stores the value accordingly by the month in a 2D temporary array, where in the one dimension there are 12 months and in the another dimension - appended values. Then in another loop the values are averaged within each month and result is given as 2D array as a month-averaged value pair.

The function can be seen here - http://pastebin.com/YZEtkKh6

2012-01-24

Gasoline price charts



This is chart from the cheapest E95 station in Dārzciema iela 127. This one automatically takes data from my database where data is checked and updated every 4 hours.
1)Updating database - the new records are added to database only if they are different from previous, last data. Such approach allows to save database space.
2)But in the same time the data reconstructing is needed when the data is taken out of database. For example, if I have no value changes between five days, I would not interpolate the values between records, but in all these days between I make the value as it was in the day before this no-value gap.

Link to the source code.

2011-11-29

long term monthly mean over 100 years

MS Excel is one of the most popular spreadsheet programs in the world. It is possible to write nice macros in VBA, it is handy and mostly meet a usual user needs. For me it is very convenient data storage type - but when it comes to work with large data I prefer writing macros or using external modifying. And here comes Python, again. Ok, the problem:
I have an excel document with data in one sheet, ~19 columns and daily measurements for 100 year period ~34000 rows. The A column contains date, other 19 columns counting from B - values/measurements. And I need to calculate firstly the long term monthly mean normal values for each column (resulting 12 values for each month) and secondly - percentiles over these 19 columns.
The reading from excel is done using XLRD package and percentile calculating - using scipy stats . These two libraries should be installed additionally to run my class.
The created class is so-so complete with a lot of space for possible improvements; currently it met my needs and I`m ok with that.
The feeling of satisfaction after doing such work manually in excel or creating script is completely different, and in the latter case it`s more positive.
This class is free to use, but if you are using this class, please, make the reference to me and let me know about the project where it has been used.

2011-09-17

Avoiding injections

Very fast and useful way, how to avoid different injections in websites - if you know what type of variable can be.
For example - if you have any ID`s or other values which can be only integers - in the script you can always be sure that after such validation it will always be an integer just writing (int) before the variable - like -
$variable = (int) $_GET['variable'];
 Another example - if you have variable which could always be date - you could pass it as a string and build a date object from it
$date=$_GET['date'];
$dateObj = new DateTime($date);
//if you need to get back to string in any case
$dateString = $date->format("Y-m-d") //or other format
Dealing with the float type it is very similar like integer type. But it is always necessary to remember - what is the decimal seperator of the host and decimal seperator in your country. In my case, I have to remember, that the host`s decimal seperator is dot, but usually people in Latvia are using comma. So, I have to parse the given "float string", and replace comma with dot, if there`s any.

$value=(float)str_replace(",",".",$value);

Linear interpolation. Extended version.

I already wrote about the linear interpolation and related problems which were successfully solved. However, full convenience requires some monthly statistical capacities. Usually the data series, including interpolated ones, does not start and end with first and last day of the month, respectively and thus the first thing is to cut off the first and last incomplete month measurements. And only then it is possible to split the data series by month. Splitting occures in the loop splitting the corresponding date and getting the month and comparing it with the previously splitted month value. If the measurement is in the same month, the new two dimensional array is created with the date (year and month) in the first dimension, and (measured including interpolated) value in the second. And if the current month value differs from the detected month value in previous cycle, it is time to change the first - date dimension value to the next one. After creating such array, it is time to make some statistics for each of inner measurement array.
You can try the linear interpolator here and see the source code (not yet OOP, but i hope soon) - here.

2010-12-19

Outsim Synthmaker v1.1.1.

It is very easy to build your own custom VSTi`s with this soft. It cames with good manual/tutorial where you can build a sample synth and also it has good wiki site.

I made sample synth and I was playing around it, and here I`ll build something more simple.

In the beginning I`m choosing midi input, and as I dont have any but keyboard, then I use it.

Next I`m adding two more modules - ASIO output (faster than usual ones, download here -> asio homepage) and midiToPoly. I enable PC Keyboard (it becomes red) and ASIO output (becomes blue) and connect Midi In to MidiToPoly.
I`m going to edit MidiToPoly module becouse I`ll use it as simple as I can. Double click on it and woala

There is a selector which selects frequency output type between normalized and usual one. Smaller red box shows playing classic note A and related frequencies. Larger red box shows primitives which I will delete, so I`ll directly connect Portamento frequency output to module output.
Now it becomes clear and simple.
And now - why did I delete that stuff? I want to add the sine wave oscilator primitive. And it needs normalized frequency. If it is so, then why should I use another converter to convert frequency to normalized or use that selector?
Almost finished - I`m adding stereo clip, which cuts mono values below 1 (because if it is above 1 it`s getting distorted)
And last modification - phase inverter using code. I`ll take one of StereoClip output and invert its phase using code textbox. Code textbox you can find under tab named "Other" or just search for "code"
And here it is. input has been taken and output is phase inverted source.

2010-12-16

combining two statistical parameters to get best suitability

R square value (coefficient of determinant) and root mean square error (rmse). These are typical parameters which shows, how two arrays are dependent from each other.

R square value shows tedency and is very easy calculated in MS Excel using command "=correl(array1[Ax:Ax+n];array2[Bx:Bx+n])^2" and it is calculated by formula
Equation 5. Equation. Coefficient of Determination. Uppercase R squared equals 1 minus summation under script lowercase i belongs to uppercase I open parentheses uppercase Y subscript lowercase i minus uppercase Y hat subscript lowercase i close parentheses raised to the second power divided by summation under script lowercase i belongs to uppercase I open parentheses uppercase Y subscript lowercase i minus uppercase Y bar close parentheses raised to the second power.

Root mean square error shows difference between two given data arrays. It is calculated by taking each data pair and getting their difference, and finally RMSE is equivalent to standart deviation of all these differences. Not too complicated as it seems.

R-squared is between 0 and 1. Usually RMSE is absolute and something positive (newertheless differences between data pairs still could be negative, but standart deviation always will be positive, even calculated from negative data - it is absolute deviation). But it is possible to get RMSE value relative, knowing each data array minimum and maximum values. Relative RMSE (RRMSE) = RMSE/maxx-minn

Now, both parameters are between 0 and 1.
Best suitability (BS) would be if R-squared = 1 (ascending) and RRMSE = 0 (descending)
BS = R^2-RMSE

Weights. Sometimes it is useful to give different weights to each of these statistical parameters. It`s simple - just multiply each coefficient with its weight.
I want that RMSE is twice hevier parameter than R^2.
BS = R^2 - RMSE*2

Sometimes it is useful to add some constante
BS = R^2 - RMSE + Const
but very rare.