Tuesday, August 27, 2013

Scicoslab/Scipad: Upgrading from 8.70 to 8.71

Note: This guide is for Linux.

  1. Download from source
  2. Instructions are here: installation guide
  3. However, to move files from the unzipped source to the scicoslab directory ( /usr/lib/scicoslab-gtk-4.4.1/), use
  4. sudo cp -r scipad/*  /usr/lib/scicoslab-gtk-4.4.1/
    

    The lesson here is that mv command doesn't have -r option, so cp is all right.

  5. Then start scicoslab with root privilege
  6. sudo scicoslab
    
  7. Issue the following under scicoslab
  8. genlib("utillib");
    

    Exit and restart scicoslab, and that will be okay.

Tuesday, August 20, 2013

A Hands-on Guide to ScicosLab - Part 2

Define a Zero Vector

--> v = zeros(1:20)

Use Scipad Editor

To launch the embedded editor, type

--> scipad

One can write a sequence of Scicos instructions and save it as *.sci files. To load such a program into ScicosLab, type

--> exec('foo.sci');

Directory operation

It inherits a Linux style,

--> pwd
--> ls
--> cd

To clear terminal screen, use either one

--> ctrl+l
--> clc

Define a Function

For instance,

function [re,im] = compute(v, f)
    re=re+cos(2*%pi*f*v/1000000);
    im=im+sin(2*%pi*f*v/1000000);
    return [re,im];
  endfunction

The return value is a vector [re,im], as you might notice, the Scicos language is C-like but not strong-typed. This function compute takes two params. Notice that the incremental sum is not supported, that is re+=... commits an error. Once loaded into ScicosLab, it works as library functions. You can call it anytime, but after a clear command, it vanishes as all the other customized variables do.

For and While

for i=1:20, printf("%d\n",i); end

Notice that no increment of i is needed. It does so automatically at every loop.

i=1;
while i<=20, printf("%d\n",i);i++; end

TBD.

Wednesday, August 14, 2013

A Short Recap on Fourier's Toys

Fourier's building blocks were sine and cosine waves. All the majesty of periodic functions can be constructed by them.

Fourier series

Just a glimpse. A periodic function $f(x)$, with $(-\pi, \pi)$ as its basic periodic unit, can be approached by the sum of an infinite series.

$$a_0 = \frac{1}{2\pi} \int_{-\pi}^{\pi} f(x)dx \\ a_n = \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)\cos(nx)dx \\ b_n = \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)\sin(nx)dx \\ a_0 + \sum_{n=1}^{\infty}(a_n \cos (nx) + b_n \sin (nx))$$

Fourier Transform

Also a quick glance,

$$\mathcal{F}(f(t))(s) = \int_{-\infty}^{+\infty} f(t)e^{-2\pi i s t} dt$$

Discrete Fourier Transform (DFT)

A bit into the point. The input of DFT is a finite series of equally-spaced (in time) samples. e.g., f(0), f(0.1), f(0.2), ..., f(2π), and the output is a series of complex numbers encoding the amplitude and phase information, say $\mathcal{F}(0)$, $\mathcal{F}(F_0)$, ..., $\mathcal{F}(-F_0)$. And $F_0$ is the fundamental frequency, which is calculated as

$$F_0 = \frac{1}{N \Delta}$$

$\Delta$ is the sample period, in our example it is $0.1$. The x-axis of these numbers are the frequency spaced as in the figure below,

However, the discrete transform is thus performed, $$\mathcal{F}(n) = \sum_{k=0}^{N} f(k) e^{-2\pi i n k/N}$$

Sunday, August 11, 2013

A Hands-on Guide to ScicosLab - Part 1

The basic calculation unit is a vector or a matrix in ScicosLab. What we get used to is a 1*1 matrix.

Define a Vector

--> 0:0.1:4*%pi

Well, the result is stored in the variable ans and the vector will be displayed hereafter. This vector is comprised of float numbers from 0 to 4π with a spacing of 0.1, they are 116 numbers, check this with the function length(). Yes, %pi is the Scicos way of pre-defining some constants. For the logarithmic natural base e, it uses %e. To stop printing results after each command, one can end it with a semi-colon(;).

Use Variables

--> a = 0:0.1:4*%pi;

Variables are type-free when being declared, so the initialization will make the variable a being a vector. Any assignment will change its content and type just like using a brand-new variable. The system preserved variable ans will be updated in the same fashion after executing every command.

--> a = sin(a);

Plot a Sine Wave

--> plot(a);

This gives you a sine wave of 2 periods.

Perform Discret Fourier Transform

--> v = dft(a,-1);
--> clf();
--> plot(v);

To check the usage of a function or related entries, use

--> apropos fourier
--> help dft

To clear the graph/figure, use the function clf(). We see in the new plotting

ScicosLab just drew the real part of frequency components, to compare with the imaginary parts, use

--> plot(imag(v), 'red');

The figure gets re-shaped and we see two spikes again. As a matter of fact, the Fourier transform of sine wave is two dirac functions.

Variables Revisited

--> browsevar
--> clear

The former gives a window of currently being-used variables, and the latter command just destroys them.

Friday, August 9, 2013

Read Data Text Files into A Vector in Scicoslab

The data file temp is lines of float numbers with one at each line.

--> fp = mopen("temp","r");
--> v = mfscanf(-1,fp,"%f");
--> v';

A Note on Ubuntu Dash Icons

In principle, dash application icons are located under /usr/share/icons/hicolor/apps, however, this can be customized. For instance, the configuration for a program named ScicosLab lies in /usr/share/applications/scicoslab.desktop, it has the following content:

[Desktop Entry]
Name=ScicosLab
Comment=Scientific Computing using ScicosLab
TryExec=/usr/bin/scicoslab
Exec=/usr/bin/scicoslab
Icon=/usr/lib/scicoslab-gtk-4.4.1/config/puffin-gtk48.png
Terminal=false
Type=Application
Categories=Education;Science;Math;
StartupNotify=false
X-Desktop-File-Install-Version=0.15

One can restart the system to see the effect.

To regenerate the cache

sudo gtk-update-icon-cache --force /usr/share/icons/hicolor