In my work I need samples of mathematical functions in the form of text streams. For example, I need a program which generates values of sine function at discrete time points and prints them into stdout
. Then I need to combine these sa开发者_如何转开发mples in some form, for example sum two samples shifted by some phase. So I can split my question may by two:
Is there a pretty standard way to generate samples of mathematical function, such as sine, with given parameters – frequency, phase, amplitude, time step – in the form of simple text stream with two columns: time and function value? I know that simple script in Perl/Tcl can do this work, but I'd like to know the gereric solution.
What programs can manipulate these streams? I know about
awk
, but what can I do withawk
when I have several streams as an input? For example, how can I make a sum or product of two or three sine samples?
I'm using Debian Linux and I prefer The Unix Way, when each program does only simple task and does it perfectly, and results of separate programs may be combined by another program.
Thanks.
You can do simple numeric calculations with bc
. See the man page. More complicated calculations can be done with octave
, which is a free Matlab clone.
For example this calculates the values of an interval:
$ octave -q --eval 'printf ("%f\n", [0:0.1:pi/2])'|nl|tee x.txt
1 0.000000
2 0.100000
3 0.200000
4 0.300000
5 0.400000
6 0.500000
7 0.600000
8 0.700000
9 0.800000
10 0.900000
11 1.000000
12 1.100000
13 1.200000
14 1.300000
15 1.400000
16 1.500000
This calculates the sin values:
$ octave -q --eval 'printf ("%f\n", sin([0:0.1:pi/2]))'|nl|tee y.txt
1 0.000000
2 0.099833
3 0.198669
4 0.295520
5 0.389418
6 0.479426
7 0.564642
8 0.644218
9 0.717356
10 0.783327
11 0.841471
12 0.891207
13 0.932039
14 0.963558
15 0.985450
16 0.997495
And the join
command can be used to join the two files:
$ join -1 1 -2 1 -o 1.2 2.2 x.txt y.txt
0.000000 0.000000
0.100000 0.099833
0.200000 0.198669
0.300000 0.295520
0.400000 0.389418
0.500000 0.479426
0.600000 0.564642
0.700000 0.644218
0.800000 0.717356
0.900000 0.783327
1.000000 0.841471
1.100000 0.891207
1.200000 0.932039
1.300000 0.963558
1.400000 0.985450
1.500000 0.997495
But it is probably better to stay in Octave for the whole computation:
$ octave -q --eval 'for x = .1:0.1:pi/2 ; printf ("%f %f\n", x, sin(x)); end'
0.100000 0.099833
0.200000 0.198669
0.300000 0.295520
0.400000 0.389418
0.500000 0.479426
0.600000 0.564642
0.700000 0.644218
0.800000 0.717356
0.900000 0.783327
1.000000 0.841471
1.100000 0.891207
1.200000 0.932039
1.300000 0.963558
1.400000 0.985450
1.500000 0.997495
General text manipulation programs that would be useful:
- paste or join (Merging two files together)
- combine (Preform set-like operations on lines in files)
- colrm (Remove columns)
- sort (General sorting)
- sed (Search and replace, and other ed commands)
- grep (Searching)
- awk (General text manipulation)
- tee (A T-junction. Though if you need this you're probably doing something too complex and should break it down.)
I see no problem with using a perl script to generate the values. Using a bc script would of course also be an option.
Did you have a look to bc ?
精彩评论