Virtual Machine implemented as a Domain Specific Language in Ruby

Abstract

Some time ago I created a simple programming language that was compiled to its own assembly language, a file in this assembly language could be executed in a Virtual Machine or be compiled to an executable form. Today I will present an idea how to use Domain Specific Language (DSL) created in Ruby to load and execute the assembly file just as a regular Ruby script.

Implementation

A sample of an assembly file is presented below, it's intuitive except the "int" mnemonic, it means "interruption", int 0 pops element from the stack and prints it on STDOUT. There aren't any registers, everything is done by using a stack.

bar:
    int 0
ret

foo:
    call bar
    ret

main:
    push 13
    call foo
    ret

Motion estimation in super-resolution algorithms

Abstract

One of important steps in super resolution algorithms is the estimation of initial constants, without this the algorithms will produce useless higher resolution images.

From mathematical view, we may say that during taking a photo, the original scenery is gathered in high resolution, with some movement between each image (e.g. shaking hand or satellite moving on its orbit), next psf and the noise is added, at the end the image is downscaled.

motion estimation in python, used for super resolution algorithms

To obtain higher resolution we need to somehow revert this process, to do that we need to estimate mentioned movements. In this post I will present two simple algorithms that I used to estimate the movement between images.

German tank problem

Sometimes science is applied in really amazing ways, one of examples is German tank problem. During World War II, the Allies observed that the Nazis created a new model of tank. Total number of manufactured units was unknown, but the Allies, know serial numbers of some tanks. In this case, the first produced tank has serial number starts = 1, the second has serial number = 2, etc. How the Allies could estimate total amount of manufactured tanks?

Answer may be estimated by using simple equitation. Let's assume that N = estimated total amount of enemy tanks, k = amount of observed tanks, m = maximum observed serial number. Then:

N = m - 1 + m/k

Example

We observed enemy tanks with serial numbers: 20, 23, 45, 47, 48. In this case, m=48, k=4, answer can be computed on calculator or for example in R language (I really like to use it as a powerful "calculator"):

> 48 - 1 + 48/4
[1] 59

Other use cases

The same as used for German tank problem was used to estimate amount of sold Apple's products. The customers were asked to upload their serial numbers.

Zapper built from NAND gates

Zapper, what's that?

The zapper is a simple device intended to fight against parasites that, if present in the human body, can do a lot of harm. As far as I know, it's not proved by any scientific source that it actually works, and despite time, it still stays in the zone of unproven/unscientific inventions. Personally, I think that it can work as described.

From a technical point of view, a zapper consists of a square wave generator, a resistor connected to its output that limits the power of the signal delivered to the body and two electrodes. One of the electrodes is connected to the mass of the circuit, second one is connected to mentioned resistor. Frequency can be adjusted from tens of Hz to tens of kHz.

Is it safe?

I don't claim that it is, but personally I think that it is if someone doesn't have cardiac problems. If you want to build it by yourself, you do this on your own risk.

Circuit of a zapper

Almost all Zippers are build from NE555, unfortunately I hadn't has it, so instead I used 4093 (2-input Schmitt NAND gates). The electrodes were made from aluminium foil.

The value of R1 should be 33k.

You can also visit this site to get more similar circuits including the original one.

Interesting way to check statuses returned by functions

If there is a set of bool functions, then sometimes below trick may be used to make the code that checks those values shorter:

bool status = true;
   
status &= foo_1();
status &= foo_2();
status &= foo_1();

printf(status ? "status: OK\n" : "status: NOK\n");