Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #35117 > unrolled thread

Re: Program or be Programmed !

Started byPaul E Bennett <Paul_E.Bennett@topmail.co.uk>
First post2014-12-23 01:05 +0000
Last post2014-12-24 14:33 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.lang.forth


Contents

  Re: Program or be Programmed ! Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-23 01:05 +0000
    Re: Program or be Programmed ! Andrew Haley <andrew29@littlepinkcloud.invalid> - 2014-12-23 09:30 -0600
      Re: Program or be Programmed ! Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-23 22:30 +0000
        Re: Program or be Programmed ! Paul Rubin <no.email@nospam.invalid> - 2014-12-24 00:13 -0800
          Re: Program or be Programmed ! Paul E Bennett <Paul_E.Bennett@topmail.co.uk> - 2014-12-24 14:33 +0000

#35117 — Re: Program or be Programmed !

FromPaul E Bennett <Paul_E.Bennett@topmail.co.uk>
Date2014-12-23 01:05 +0000
SubjectRe: Program or be Programmed !
Message-ID<cfrte2Frt26U1@mid.individual.net>
Paul E Bennett wrote:

> WJ wrote:
>
>> Paul E Bennett wrote:
>> 
>>> I was displaying a sine-wave of
>>> asterix characters scrolling continuously down the screen. 3 lines of
>>> code generated the sine-wave (and cosine) numbers and the other 30 or so
>>> lines did scaling and arrangements for its display. I was showing the
>>> source code in a separate window.
>> 
>> Gauche Scheme:
>> 
>> (define line-length 79)
>> (define mid-point (div line-length 2))
>> (define one-line (string-append
>>   (make-string mid-point #\space) "|" (make-string mid-point #\space)))
>> (define (pause) (sys-nanosleep 5e7))
>> (let loop ((x 0.0))
>>   (when (< x 22)
>>     (let ((this-line (string-copy one-line))
>>           (p (round->exact (+ mid-point (* mid-point (sin x))))))
>>       (string-set! this-line p #\*)
>>       (print this-line)
>>       (pause))
>>     (loop (+ 0.1 x))))
>> 
>> Partial output:
>
> [%X]
> 
> Ok, now do that without using the sin x function (which I did not in my 
> example). I shall post the full source of my example shortly. I have just 
> had a quick look and there is a total of 65 lines defining 7 words
> including the comments and a more vertical style than most use. You will 
> see (eventually) that had I used a more horizontal style it would probably
> have been just seven lines.
> 
> The reason I started playing with the central sine-wave generation was for
> a small robotic project that required me to generate such a waveform. The 
> three line sine-wave generation code is all that I am using from this 
> example for the robotics project.

In the light of no answer from WJ I am going to leave the following code as 
a gift. The code has a few controls.

n TO OFFSET will shift the centre-line of the displayed waveform.
n TO AMPLITUDE will adjust the scaling
CYCLE will produce one waveform cycle
n CYCLES will perform n cycles.
CONTINUOUS will continuously produce the waveform until any key is pressed. 
It will always stop at the end of a completed cycle.

\ Sample Code

(( This code generates a sine-wave to the screen displayed as a star 
character 
eat a distance from th left hand edge proportional to the amplitude of that 
point of the sine wave. ))

Vocabulary TRACE ONLY Forth Also Trace trace definitions

: (DDA) ( S: sin\cos -- sin'\cos' )
( This code is the main sine wave generator )
  tuck over 
  256 / + -rot
  swap 256 / - swap
;

: DDA      \ "<spaces> name"
\ G: Create a DDA Instance with local storage for 2 Cells to 
\    accommodate the SINE and COSINE values at each step.
  CREATE 0 , 32768 ,  \ S: " spaces name" Sine-seed\Cosine-seed
  DOES> DUP 2@ (DDA) ROT 2!  \ S: --
;

: DDA-RESET   \ S: 'addr --
  32768 0 
  rot >Body 2! ;

DDA SINE   \ The child word SINE will perform the DDA actions when called.

\ **************************************************************
\ Screen Oscilloscope Functions
\ This lexicon is for a single channel Oscilloscope function that
\ takes a value and plots the wave-form to the screen. The plotted
\ character is an asterix.
\ **************************************************************

36 VALUE OFFSET
32 VALUE AMPLITUDE

: level ( S: n -- 'n )
( This code adjusts the scale of the signal to ensure it will be in )
( range for printing to screen )
  AMPLITUDE 32768 */ OFFSET + 0 MAX
;
  
: .scope ( S: n -- )
( This code is the portion that plots the text on the screen at the )
( appropriate distance from the left hand edge )
  cr level spaces ." *"
;

: chunk ( S: -- )
( This performs several blind cycles to reduce the height of screen )
( over which the waveform is displayed. )
  35 0
  do   SINE
  loop
  100 ms
;

: cycle ( S: -- )
\ Perform one full cycle
  46 0 
  do   chunk              \ 46 * 35 = 1610; the no. of steps in full cycle.
       ['] sine >body @ 
       .scope
  loop
  ['] SINE DDA-RESET
;

: cycles ( S: n -- )
( Displays the n cycles of the sine wave )
  0
  do   cycle
  loop
;

: continuous ( S: -- )
( Display the sine wave as a continous wave. Any key press will exit )
( the routine )
  begin cycle
        key?
  until
  key drop
;


-- 
********************************************************************
Paul E. Bennett IEng MIET.....<email://Paul_E.Bennett@topmail.co.uk>
Forth based HIDECS Consultancy.............<http://www.hidecs.co.uk>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

[toc] | [next] | [standalone]


#35137

FromAndrew Haley <andrew29@littlepinkcloud.invalid>
Date2014-12-23 09:30 -0600
Message-ID<OcydnVONz8oAFwTJnZ2dnUU7-a2dnZ2d@supernews.com>
In reply to#35117
Paul E Bennett <Paul_E.Bennett@topmail.co.uk> wrote:
> 
> : (DDA) ( S: sin\cos -- sin'\cos' )
> ( This code is the main sine wave generator )
>  tuck over 
>  256 / + -rot
>  swap 256 / - swap
> ;
> 

Very pretty.  d/dx sin = cos, d/dx cos = -sin.  The error builds up
very fast, but you reset the DDA every cycle so that doesn't matter.
This is very appealing to me, and it would have been to me as a child
too.

Andrew.

[toc] | [prev] | [next] | [standalone]


#35158

FromPaul E Bennett <Paul_E.Bennett@topmail.co.uk>
Date2014-12-23 22:30 +0000
Message-ID<cfu8olFga0mU1@mid.individual.net>
In reply to#35137
Andrew Haley wrote:

> Paul E Bennett <Paul_E.Bennett@topmail.co.uk> wrote:
>> 
>> : (DDA) ( S: sin\cos -- sin'\cos' )
>> ( This code is the main sine wave generator )
>>  tuck over
>>  256 / + -rot
>>  swap 256 / - swap
>> ;
>> 
> 
> Very pretty.  d/dx sin = cos, d/dx cos = -sin.  The error builds up
> very fast, but you reset the DDA every cycle so that doesn't matter.
> This is very appealing to me, and it would have been to me as a child
> too.
> 
> Andrew.

Thanks Andrew. It took me a little while reading some of the papers on the 
subject but having tried it out and decided to reset every cycle it gives a 
very nice and consistent waveform.

Comments on the rest of the code and the coding style (which is my rough-out 
standard) are welcome.

-- 
********************************************************************
Paul E. Bennett IEng MIET.....<email://Paul_E.Bennett@topmail.co.uk>
Forth based HIDECS Consultancy.............<http://www.hidecs.co.uk>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

[toc] | [prev] | [next] | [standalone]


#35176

FromPaul Rubin <no.email@nospam.invalid>
Date2014-12-24 00:13 -0800
Message-ID<7x8uhxihxz.fsf@ruckus.brouhaha.com>
In reply to#35158
Paul E Bennett <Paul_E.Bennett@topmail.co.uk> writes:
>>> : (DDA) ( S: sin\cos -- sin'\cos' )
>>> ( This code is the main sine wave generator )
>>>  tuck over
>>>  256 / + -rot
>>>  swap 256 / - swap
>>> ;
> Comments on the rest of the code and the coding style (which is my rough-out 
> standard) are welcome.

: h* ( n -- n ) 8 rshift ;
: (DDA) ( sin cos -- sin' cos') 2dup h* >r h* - swap r> + swap ;

[toc] | [prev] | [next] | [standalone]


#35187

FromPaul E Bennett <Paul_E.Bennett@topmail.co.uk>
Date2014-12-24 14:33 +0000
Message-ID<cg016fFtk4aU1@mid.individual.net>
In reply to#35176
Paul Rubin wrote:

> Paul E Bennett <Paul_E.Bennett@topmail.co.uk> writes:
>>>> : (DDA) ( S: sin\cos -- sin'\cos' )
>>>> ( This code is the main sine wave generator )
>>>>  tuck over
>>>>  256 / + -rot
>>>>  swap 256 / - swap
>>>> ;
>> Comments on the rest of the code and the coding style (which is my
>> rough-out standard) are welcome.
> 
> : h* ( n -- n ) 8 rshift ;
> : (DDA) ( sin cos -- sin' cos') 2dup h* >r h* - swap r> + swap ;

Thanks Paul,

Just tried out the differences. My version was 58 bytes (22 instructions) in 
Vfxlin. Yours was 4 bytes (2 instructions) for h* and 22 bytes (10 
instructions) for (DDA). I might make that change and see how it does speed-
wise.

-- 
********************************************************************
Paul E. Bennett IEng MIET.....<email://Paul_E.Bennett@topmail.co.uk>
Forth based HIDECS Consultancy.............<http://www.hidecs.co.uk>
Mob: +44 (0)7811-639972
Tel: +44 (0)1235-510979
Going Forth Safely ..... EBA. www.electric-boat-association.org.uk..
********************************************************************

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web