Thursday, 23 April 2009

Built-in Functions in Scilab

Most built in functions are identical in Matlab and Scilab. Some of them have a slightly different syntax. Here is a brief, partial list of commands with significant different syntax.

Matlab Scilab "equivalent"

all and

any or

balance balanc

clock unix('date')

computer unix_g('machine')

cputime timer

delete unix('rm file')

dir unix_g('ls')

echo mode

eig spec or bdiag

eval evstr

exist exists + type

fclose file('close')

feof

ferror

feval evstr and strcat

filter rtitr

finite (x < %inf)
fopen file('open') fread read fseek file ftell fwrite writeb global home isglobal isinf(a) a == %inf isnan(a) a ~= a isstr(a) type(a) == 10 keyboard pause + resume lasterr lookfor apropos more lines pack stacksize pause halt qz gspec+gschur randn rand rem modulo setstr code2str strcmp(a,b) a == b uicontrol uimenu getvalue unix unix_g version which whereis nargin [nargout,nargin]=argn(0) nargout

You will get a detailed tutorial on Scilab from: http://www-irma.u-strasbg.fr/~sonnen/SCILAB_HELP/frame.html

querrymail@gmail.com

Scilab: An Alternative to MATLAB

What is Scilab?

Developed at INRIA, SCILAB has been developed for system control and signal processing applications. It is freely distributed in source code format. The following points will explain the features of SCILAB. It is a similar to MATLAB, which is not freely distributed. It has many features similar to MATLAB.
Scilab is made of three distinct parts:

  • An interpreter
  • Libraries of functions (Scilab procedures)
  • Libraries of Fortran and C routines.

SCILAB has an inherent ability to handle matrices (basic matrix manipulation, concatenation, transpose, inverse etc.,)

Scilab has an open programming environment where the creation of functions and libraries of functions is completely in the hands of the user.
Scilab has an open programming environment where the creation of functions and libraries of functions is completely in the hands of the user

Download Scilab:
http://www.scilab.org/download/index_download.php?page=release#windows

What are the main differences between Scilab and MATLAB?
Functions

Functions in Scilab are NOT Matlab m-files but variables. One or several functions can be defined in a single file (say myfile.sci). The name of of the file is not necessarily related to the the name of the functions. The name of the function(s) is given by

function [y]=fct1(x)
...
function [y]=fct2(x)
...

The function(s) are not automatically loaded into Scilab. Usually you have to execute the command getf("myfile.sci") before using it.

Functions can also be defined on-line (or inside functions) by the command deff.

To execute a script file you must use exec("filename") in Scilab and in Matlab you just need to type the name of the file.
Comment lines

Scilab comments begins with: //

Matlab comments begins with: %

Variables

Predefined variables usually have the % prefix in Scilab (%i, %inf, ...). They are write protected.

Strings

Strings are considered as 1 by 1 matrices of strings in Scilab. Each entry of a string matrix has its own length.

Boolean variables

Boolean variables are %T, %F in Scilab and 0, 1 in Matlab. Indexing with boolean variables may not produce same result. Example x=[1,2];x([1,1]) [which is NOT x([%T,%T])] returns [1,1] in Scilab and [1,2] in Matlab. Also if x is a matrix x(1:n,1)=[] or x(:)=[] is not valid in Matlab.

Polynomials

Polynomials and polynomial matrices are defined by the function poly in Scilab (built-in variables). They are considered as vectors of coefficients in Matlab.

Empty matrices

[ ]+1 returns 1 in Scilab and [ ] in Matlab.

Plotting

Except for the simple plot and mesh (plot3d) functions, Scilab and Matlab are not compatible.

Scicos

Scicos (Scilab) and Simulink (Matlab) are not compatible.

querrymail@gmail.com


IR Tracking Robot

I got idea from EFY magazine and implemented along with my friends. It works well.

AMV: A Monostable Multivibrator

The robot described here senses a 38 kHz IR radiations and moves towards that direction. The system consists of three sections viz., sensor, controller and driver. The sensing section detects the 38 kHz IR radiation. The controller section processes the information from the sensor and provides the input to the driver section which has stepper motors for driving the robot.

The output of the sensors is fed to the monostable multivibrator which serves as the input to the microcontroller. Depending on the input sequence obtained the microcontroller performs sequential operation and gives out its decisions, which is a sequence of bits to drive stepper motors.

Since the microcontroller output is not sufficient to drive the stepper motor, a high voltage, high current Darlington array has been used to drive the motors. In the process of reaching the target, if an obstacle is encountered, the robot changes its path and again starts tracking the incoming IR radiation.

Robot Sensing Section


Robot Driving Section







Program

;Program Starts Here
$MOD51
ORG 0000H
CLR A
MOV P1, A
MOV P2, A
MOV P3, #0FFH
MOV R1, #11H
MOV R2, #11H
BACK: MOV C, P3.2
JB P3.0, NEXT
CPL C
NEXT: ANL C, P3.1
JC STRAIGHT
MOV C, P3.1
ANL C, P3.2
CPL C
ANL C, P3.0
JC LEFT
MOV C, P3.2
ORL C, /P3.1
ANL C, /P3.0
JC RIGHT
LJMP BACK
LEFT: MOV R3, #04H
FIRST: MOV A, R2
MOV P2, A
ACALL DELAY
RL A
MOV R2, A
DJNZ R3, FIRST
LJMP BACK
RIGHT: MOV R4, #04H
SECOND: MOV A, R1
MOV P1, A
ACALL DELAY
RR A
MOV R1, A
DJNZ R4, SECOND
LJMP BACK
STRAIGHT: MOV R3, #04H
THIRD: MOV A, R1
MOV P1, A
RR A
MOV R1, A
MOV A, R2
MOV P2, A
RL A
MOV R2, A
ACALL DELAY
DJNZ R3, THIRD
LJMP BACK
DELAY: MOV R6, #64
H1: MOV R7, #255
H2: DJNZ R7, H2
DJNZ R6, H1
RET
END

;Program Ends Here

Source: EFY Magazine

querrymail@gamail.com

Thursday, 16 April 2009

Analyzing a .wav file

In the previous article we found how to create a simple audio music file. Here, we are going to analyze it, i.e., to find out the fundamental frequency and other parameters. The code given below plots the .wav file and it power spectrum.

%Code Starts here

[y, Fs] = wavread(file); % y is sound data, Fs is sample frequency.
t = (1:length(y))/Fs; % time

ind = find(t>0.1 & t<0.12); % set time duration for waveform plot
figure; subplot(1,2,1)
plot(t(ind),y(ind))
axis tight
title(['Waveform of ' file])

N = 2^12; % number of points to analyze
c = fft(y(1:N))/N; % compute fft of sound data
p = 2*abs( c(2:N/2)); % compute power at each frequency
f = (1:N/2-1)*Fs/N; % frequency corresponding to p

subplot(1,2,2)
semilogy(f,p)
axis([0 4000 10^-4 1])
title(['Power Spectrum of ' file])

%Code ends here

querrymail@gmail.com

Generate Music using MATLAB

This is an interesting, but simple experiment which has many utility. The following code can genrate a .wav file to play a sound of required frequency.

Before proceeding you should set the following:
filename---> give a name to your fantastic music file (say 'mymusic.wav')
f-----------> fundamental frequency (say f=256) in Hz.
d----------> time duration (say t =5) in seconds.
p----------> it is vector of amplitudes of length n. (say it values be [1 0.8 0.1 0.04]

%Code starts here

Fs=22050; nbits=8; % frequency and bit rate of wav file
t = linspace(1/Fs, d, d*Fs); % time
y = zeros(1,Fs*d); % initialize sound data
for n=1:length(p);
y = y + p(n)*cos(2*pi*n*f*t); % sythesize waveform
end
y = .5*y/max(y); % normalize. Coefficent controls volume.
wavwrite( y, Fs, nbits, filename)

%Code ends here

querrymail@gmail.com

Addition of two numbers

data segment
msg1 db "Enter the first 2 digit number: $"
msg2 db 0dh,0ah,"Enter the second 2 digit number: $"
msg3 db 0dh,0ah,"Sum = $"
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov al,01h
int 21h
endm

display macro num
mov dl,num
mov al,02h
int 21h
endm

code segment
start:mov ax,data
mov ds,ax

print msg1
read
mov bh,al
read
mov bl,al
print msg2
read
mov ch,al
read
mov cl,al

mov ax,bx
add al,cl
aaa
mov bl,al
mov al,ah
add al,ch
aaa
mov cx,ax

add cl,30h
add ch,30h
add bl,30h
print msg3
display ch
display cl
display bl

code ends
end start

querrymail@gmail.com

Palindrome

This is a program in 8086 ASM to check the given string is palindrome or not.
The program is compiled in MASM compiler.

data segment
msg1 db "Enter the string: $"
msg2 db 0ah,0dh,"Reversed string is: $"
msg3 db 0dh,0ah,"Not palindrome...........$"
msg4 db 0dh,0ah," palindrome...........$"
ent db 0dh,0ah,"$"
str1 db 100 dup(?)
rev db 100 dup(?)
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov ah,01h
int 21h
endm

code segment
start:
mov ax,data
mov ds,ax
mov bx,0000h
lea si,str1
print msg1

L1:
read
mov [si],al
inc bx
inc si
cmp al,0dh
jnz L1


dec si
mov cx,bx
lea di,rev

L2:
mov al,[si]
mov [di],al
inc di

dec si
dec cx
jnz L2

print msg2
add si,bx

mov cx,bx
print ent

L3:
mov al,[si]
mov dl,al
mov ah,02h
int 21h
dec si
dec cx
jnz L3

sub di,bx
mov cx,bx

L4:
dec cx
jz p
inc si
inc di
mov al,[si]
cmp al,[di]
jz L4
print msg3 ;not paliandrome
jmp en

p:
print msg4 ;paliendrome
en:
mov ah,4ch
int 21h
code ends
end start

ret


querrymail@gmail.com