A DSP task in Erlang ?

Corrado Santoro <>
Tue Jan 20 17:11:56 CET 2004


Hi Massimo, 
maybe do you need something like this? 
 
-module(l). 
-export ([add/2, add_list/1]). 
 
add ([H1], [H2]) -> 
  [H1 + H2]; 
add ([H1 | T1], [H2 | T2]) -> 
  [ H1 + H2 | add (T1, T2)]. 
 
add_list ([A , B]) -> 
  add (A, B); 
add_list ([H | T]) -> 
  add (H, add_list(T)). 
 
16> l:add([1,2,3],[4,5,6]). 
[5,7,9] 
17> l:add_list([[1,2,3],[4,5,6],[7,8,9],[11,12,13],[1,2,3]]). 
[24,29,34] 
 
or the following version that takes advantage of tail recursion? 
 
-module(ll). 
-export ([add/2, add_list/1]). 
 
add (L1, L2) -> 
  lists:reverse (add (L1, L2, [])). 
 
add ([H1], [H2], Acc) -> 
  [H1 + H2 | Acc]; 
add ([H1 | T1], [H2 | T2], Acc) -> 
  add (T1, T2, [H1 + H2 | Acc]). 
 
add_list ([A , B]) -> 
  add (A, B); 
add_list ([H | T]) -> 
  add (H, add_list(T)). 
 
 
The complexity should be O(n*m), with n=number of lists and m=size of each 
list. This means that the execution time should be predictable. 
 
Is this what you need? 
 
Ciao, 
-Corrado 
 
Quoting Massimo Cesaro <>: 
 
> Hi, 
> I'm trying to simulate a DSP operation in Erlang, but I have 
> difficulties in figuring out the best way to write it. 
> My aim is to be able to sum the contents of several lists, e.g.: 
> if A = [1,2,3], and B = [1,2,3] and C = [1,2,3], I'd like to findaa fast 
> end efficient way to get a resulting list summing the elements such as D 
> = [3, 6, 9], i.e. the first element of the result is the sum of the 
> first element of the lists, etc. 
> I checked with list comprehension, but I found that they are useful to 
> write generators rather than adders: 
>  
> Erlang (BEAM) emulator version 5.2 [source] [hipe] 
>  
> Eshell V5.2  (abort with ^G) 
> 1> A = [1,2,3]. 
> [1,2,3] 
> 2> B=[1,2,3]. 
> [1,2,3] 
> 3> C=[1,2,3]. 
> [1,2,3] 
> 4> [X+Y+Z || X <- A, Y <- B, Z <- C]. 
> [3,4,5,4,5,6,5,6,7,4,5,6,5,6,7,6,7,8,5,6,7,6,7,8,7,8,9] 
> 5> 
>  
> clearly not what I need. 
>  
> The "best way" I'm looking for is something that could be used in a soft 
> real time system. Given a fixed input length of lists, I'd like to have 
> a predictable and constant time of execution of the function. 
> Anybody have suggestions on this ? 
> Regards, 
> Massimo 
>  
>  
>  
>  
>  
 
 
 
--  
====================================================== 
Eng. Corrado Santoro, Ph.D. 
 
University of Catania - Engineering Faculty 
Department of Computer Science and 
Telecommunications Engineering 
Viale A. Doria, 6 - 95125 CATANIA (ITALY) 
 
Tel: +39 095 7382364           Fax: +39 095 338280 
     +39 095 7382365 
     +39 095 7382380 
 
EMail:  
Personal Home Page: 
            http://www.diit.unict.it/users/csanto 
 
NUXI Home Page: 
            http://nuxi.iit.unict.it 
====================================================== 

-------------------------------------------------
This mail sent through IMP: http://www.cdc.unict.it/



More information about the erlang-questions mailing list