Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

resample function in SciLab

2,221 views
Skip to first unread message

Kamil

unread,
Jul 10, 2006, 12:37:22 PM7/10/06
to
there is a resample function in matlab, is there a similar function for
scilab?

i dont see any toolboxes that would contain this function, please help

Alan

unread,
Jul 12, 2006, 3:03:31 AM7/12/06
to
Hello,

In scilab, you have the intdec function, included in the
Signal Processing toolbox (see help intdec). For exemple,
for an upsample factor of 3 :

x=0:0.05:1;x=sin(10*t);
scf(0);plot(x);
y=intdec(x,3);
scf(1);plot(y);

This function realizes insertion+low-pass filter (if second argument
is >0) or decimation+low-pass filter (if second argument is <0).
For both upsample and downsample, this operations are realized in
frequency domain of the original signal to resample.
You can see scilab instructions of this macros in the directory
SCI/macros/signal/intdec.sci which uses FFT function.

If you only want do an insertion or a decimation, you can easily
do it in time domain without any help of some function.
For exemple, for a resample of a signal with an upsample factor
of 3 with copy of value :

x=0:0.05:1;x=sin(10*t);
scf(0);plot(x);
y=x .*. [1 1 1];
scf(1);plot(y);

and to do zero padding between samples :

x=0:0.05:1;x=sin(10*t);
scf(0);plot(x);
y=x .*. [1 0 0];
scf(1);plot(y);

Hope That Helps
Alan

Alan

unread,
Jul 12, 2006, 3:10:00 AM7/12/06
to
Sorry,

t=0:0.05:1-0.05;

and not

x=0:0.05:1;

@+

Alan

unread,
Jul 12, 2006, 3:16:23 AM7/12/06
to
Re sorry,

...insertion+low-pass filter (if second argument
is >1) or decimation+low-pass filter (if second argument is <1).

and not

... insertion+low-pass filter (if second argument


is >0) or decimation+low-pass filter (if second argument is <0).

Kamil

unread,
Jul 12, 2006, 1:18:05 PM7/12/06
to
Thanks Alan! I didnt know about that function at all, unfortunately, it
doesnt help me in my case. The signal that i need to upsample, by a
factor of 4, contains imaginary numbers in the matrix, but intdec does
not support imaginary numbers!:(

i have started working on another resamp function. I am having problems
converting one of the functions though, fir1, which is not supported in
scilab either.

I would greatly appreciate it if someone can help me convert this
function to scilab.

here is the converted sci file, all that needs work on is the fir1(at
the very last line of this function) function.

thnx again guys


function [y] = resamp(x,r)

// Ouput variables initialisation (not found in input variables)
y=[];

// Display mode
mode(0);

// Display warning for floating point exception
ieee(1);

// RESAMP Resample an input sequence x by a factor of r
// to produce an output sequence y by a combination
// of upsampling and downsampling.
// For example,
// y=resamp(x,1.5);
// will upsample x by 3 and downsample by 2.

// !! L.9: Matlab function rat not yet converted, original calling
sequence used
// L.9: Name conflict: function name changed from rat to %rat
[L,M] = rat(r);
// ! L.10: real(mtlb_double(L)*max(size(mtlb_double(x)))) may be
replaced by:
// ! --> mtlb_double(L)*max(size(mtlb_double(x))) if
mtlb_double(L)*max(size(mtlb_double(x))) is Real
xe = zeros(1,real(mtlb_double(L)*max(size(mtlb_double(x)))));
xe = mtlb_i(xe,mtlb_imp(1,mtlb_double(L),$),x);
if
bool2s(mtlb_logic(mtlb_double(L),">",1))|bool2s(mtlb_logic(mtlb_double(M),">",1))
then
h = lowpass(L,mtlb_min(%pi/mtlb_double(M),%pi/mtlb_double(L)));
xi = mtlb_conv(xe,h);
// Get rid of trailing and leading data so input and output
// signals line up temporally: (N even, length(h)=N+1 is odd)
xi = xi((max(size(h))+1)/2:$-(max(size(h))-1)/2);
else
xi = xe;
end;
y = xi(mtlb_imp(1,mtlb_double(M),$));
endfunction

function [h] = lowpass(gain,wc)

// Ouput variables initialisation (not found in input variables)
h=[];

// Display mode
mode(0);

// Display warning for floating point exception
ieee(1);

// LOWPASS Compute the impulse response of a low pass filter with
// gain """"gain"""" and cut frequency """"wc"""" using a
Kaiser window
//with ripple delta=0.001 and dw = 0.1*pi.

dw = 0.1*%pi;
delta = 0.001;
A = -20*log10(delta);
beta =
(0.1102*(A-8.7))*(A>50)+((0.5842*((A-21)^0.4)+0.07886*(A-21))*(A<=50))*(A>=21);

N = ceil((((A-8)/2.285)/dw)/2)*2;// Divide and mult by 2 to make N even
// !! L.12: Matlab toolbox(es) function kaiser not converted, original
calling sequence used
// !! L.12: Matlab toolbox(es) function fir1 not converted, original
calling sequence used
h = mtlb_double(gain)*mtlb_double(wfir('bp',N,[0
wc/%pi],'kr',window('kr',N+1,beta)));
endfunction

Please help!!:(

thanx in advance!

Alan

unread,
Jul 13, 2006, 6:27:30 AM7/13/06
to
Hello Kamil,

I think that i've found the pitfall in the fir1
function convertion.

With more investigation and comparison between
matlab and scilab it seems that :
fir1(N,wn) in matlab
is equivalent to
wfir('lp',N,[wn/2 0],...)
or wfir('bp',N,[0 wn/2],...) in scilab.

Thus, it exist's a division factor of 2 for frequency cut-off
between scilab and matlab.

I've a little bit reviewed your preceding two functions
(see the end of this post).

t=0:0.05:1-0.05;
w=10;
x=exp(%i*w*t);
y=resamp(x,1.5);
scf(0);plot(real(y));

The result seems to be in accordance with matlab.

HTHelp
Alan

function [y] = resamp(x,r)
y=[];
[L,M] = rat(r);
xe = zeros(1,real(L*max(size(x))));
xe = mtlb_i(xe,mtlb_imp(1,L,$),x);
if (L>1)|(M>1) then
h = lowpass(L,min(%pi/M,%pi/L));
xi = convol(xe,h);


xi = xi((max(size(h))+1)/2:$-(max(size(h))-1)/2);
else
xi = xe;
end;

y = xi(mtlb_imp(1,M,$));
endfunction

function [h] = lowpass(gain,wc)
h=[];


dw = 0.1*%pi;
delta = 0.001;
A = -20*log10(delta);

beta = (0.1102*(A-8.7))*(A>50)+..


((0.5842*((A-21)^0.4)+0.07886*(A-21))*(A<=50))*(A>=21);
N = ceil((((A-8)/2.285)/dw)/2)*2;

h = gain*wfir('lp',N,[wc/(2*%pi) 0],'kr',window('kr',N+1,beta));
endfunction

Kamil

unread,
Jul 13, 2006, 8:59:26 AM7/13/06
to
ALAN!!! THANK YOU!!! you are a life saver!!!!
ive been looking at this for a whole day and i didnt even think about
the cutoff frequency being wrong.

Thank you soo much Alan!!

divid

unread,
Jul 13, 2006, 5:21:08 PM7/13/06
to
How did you get this Scilab code?
Have you used the crazy Matlab2Scilab "translator"?
It looks like a joke!

Kamil wrote :

Kamil

unread,
Jul 14, 2006, 8:44:56 AM7/14/06
to
yeah i did use that crazy math2sci translator, there is no way in hell
i would write all those crazy "mtlb_" conversions, but hey, it WORKS!
0 new messages