/************************************************************************

 File: C.PLOTUTIL

 Inhoud:
         void gen_signal_background(VENSTER *vp),
         void gen_freq_background(VENSTER *vp),
         void gen_bode_background(VENSTER *vp),
         void gen_magnitude_background(VENSTER *vp),
         void gen_phase_background(VENSTER *vp),
         void gen_hist_background(VENSTER *vp),
         void gen_signal_plot(VENSTER *vp),
         void gen_bode_plot(VENSTER *vp,int mid_type),
         void gen_magnitude_plot(VENSTER *vp,int mid_type),
         void gen_phase_plot(VENSTER *vp,int mid_type),
         void gen_hist_plot(VENSTER *vp);
         void make_plot(VENSTER *vp);

  Schrijvers: Edwin Zoer & Erwin Waterlander
              HTS Enschede
  Datum     : juni 1993

 ************************************************************************/


#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>

#include "allegro.h"
#include "image.h"
#include "plotutil.h"
#include "asm.h"

#define PI M_PI

#define MIN2(x,y)          ((x < y) ? x : y)
#define MAX2(x,y)          ((x > y) ? x : y)

/* Een paar hulp-getallen die de witte randen om de plot bepalen  */

#define SIGNAL_HOOGTE      250    /* Aantal pixels verticaal voor signaal */
#define BODE_SIGNAL_HOOGTE 200    /* Aantal pixels verticaal voor signaal */
#define PH_MAG             3.0    /* Verhouding tussen phase/magn bij bodediagram */
#define PHASE_MAX          200
#define PHASE_MIN         -200



/* Het filtype van een drawfile is 0AFF. Deze wordt gebruikt bij saven    */

#define FILETYPE_DRAWFILE          0x0aff



char font_name[256];
extern VENSTER *vp[MAXSIG];
extern unsigned short MAX_LENGTH;
extern unsigned short SAMPLE_RATE;
extern BOOL bon;
extern int max_signals;

BITMAP *asm_draw_bitmap;


/**********************************************************************************
 *                                                                                *
 * gen_signal_background genereerd het assenstelsel, met bijbehorende tekst, deze *
 * achtergrond verschild per domein                                               *
 *                                                                                *
 **********************************************************************************/

#define pixel_afst 100
#define maxy Window_Height(vp)


void gen_signal_background(VENSTER *vp)
{
	int tel;
	int lengte,hoogte,aantaltxt;
	double totaal_tijd,delta_t,delta_y,Max_abs;
	double eenheid,y_waarde[20],afstand;
	double *xwaarde;
	char ch[256],eenhstr,nm[40];
	ASM_SAMPLE *s;                        /* pointer naar te displayen data */
	double Minimum, Maximum;              /* min en max waarden van record */


	strncpy(nm,"Signal ",10);
	strncat(nm,DataSignalName(vp),40);

	switch (Window_Mode(vp))
	{
	case REAL_M:
		strncat(nm,"  Real (Time)",20);
		break;
	case IMAG_M:
		strncat(nm,"  Imaginary (Time)",20);
		break;
	case MAGN_M:
		strncat(nm,"  Magnitude (Time)",20);
		break;
	default:
		break;
	}

	lengte  = Window_Width(vp) - LEFT_XOFFS - RIGHT_XOFFS;
	hoogte  = Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;
	afstand = pixel_afst;

	aantaltxt   = (int) ((lengte/afstand)+1);
	totaal_tijd = (double)(DataLength(vp))/(double)(DataSampleRate(vp)*10);
	delta_t     = (double)(totaal_tijd)/(double)(aantaltxt-1);
	afstand     = (double)(lengte)/(double)(aantaltxt-1);

	eenheid=1.0;
	eenhstr=' ';


	if (totaal_tijd < (1))
	{
		eenheid=1e-3;
		eenhstr='m';
	}
	if (totaal_tijd < (1e-3))
	{
		eenheid=1e-6;
		eenhstr='u';
	}
	if (totaal_tijd < (1e-6))
	{
		eenheid=1e-9;
		eenhstr='n';
	}
	if (totaal_tijd < (1e-9))
	{
		eenheid=1e-11;
		eenhstr='p';
	}

	textout_ex(asm_draw_bitmap,font,nm,(lengte/2),0,ASM_EMPH_COLOR, -1);

	sprintf(ch,"--> Time [%cs]",eenhstr);
	textout_ex(asm_draw_bitmap,font,ch,(lengte/2),maxy-text_height(font),ASM_GRAY_COLOR, -1);


	delta_t=delta_t/eenheid;                /*  totaal_tijd nu  x.xxxx */

	/* print 0  x-as, y-as  text*/

	sprintf(ch,"0");
	textout_ex(asm_draw_bitmap,font,ch,LEFT_XOFFS,maxy-28,ASM_GRAY_COLOR, -1);

	/* print record_nr en channel_nr  */

	sprintf(ch,"Rec: %d - Chan: %d",Window_Record(vp),Window_Channel(vp));
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS+lengte-200),(TOP_YOFFS-10),ASM_GRAY_COLOR, -1);


	/* allocate data for xwaarde */
	if ((xwaarde = (double *) malloc(aantaltxt*(sizeof(double)))) == NULL)
	{
		asm_wprintf ("Error: malloc failed in gen_signal_background (xwaarde)");
	}
	else
	{
		for(tel=1;tel < aantaltxt;tel++)
		{
			xwaarde[tel]=(delta_t*tel);
			sprintf(ch,"%3.2f",xwaarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(int)(tel*afstand+LEFT_XOFFS-25),(int)(maxy-28),ASM_GRAY_COLOR, -1);
		}
		free(xwaarde);
	}

	/* teken vertikaal grid  */

	for(tel=1;tel < aantaltxt;tel++)
		line(asm_draw_bitmap,LEFT_XOFFS+tel*afstand,(TOP_YOFFS),LEFT_XOFFS+tel*afstand,(hoogte+TOP_YOFFS),ASM_GRAY_COLOR);


	/* teken de x en y as in zwart (+ boven en rechts)   */

	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , LEFT_XOFFS       , maxy-BOTTEM_YOFFS, ASM_BLACK_COLOR);
	line(asm_draw_bitmap, lengte+LEFT_XOFFS, TOP_YOFFS          , lengte+LEFT_XOFFS, maxy-BOTTEM_YOFFS, ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , (maxy-BOTTEM_YOFFS), lengte+LEFT_XOFFS, maxy-BOTTEM_YOFFS, ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , lengte+LEFT_XOFFS, TOP_YOFFS        , ASM_BLACK_COLOR);



	/* Bepaal VSCALE m.b.v. Maximum en minimum     */



	if (Window_Mode(vp)==REAL_M)
	{
		s = DataReal(vp);     /* display real data */
	}

	else
	{
		s = DataImag(vp);     /* display imaginary data */
	}

	Minimum=Maximum=*s;                       /* Min en Max bepalen */

	for(tel=0;tel < (DataLength(vp));tel++)
	{
		if (*s > Maximum) Maximum=*s;
		if (*s < Minimum) Minimum=*s;
		s++;
	}



	if ((Maximum <= 1)  && (Maximum >= 0.0)) Maximum=1;
	if ((Minimum >= -1) && (Minimum <= 0.0)) Minimum=-1;

	Window_Max(vp) = Maximum;
	Window_Min(vp) = Minimum;

	aantaltxt=9;
	afstand = (int)(-hoogte/(aantaltxt-1));

	Max_abs = (double)MAX2(fabs(Maximum),fabs(Minimum)); /* Absolute maximum */

	delta_y = Max_abs*2/(aantaltxt-1);

	Window_VScale(vp) = Max_abs/(hoogte/2);

	if (Max_abs >= USHRT_MAX)
	{
		for(tel=0;tel < aantaltxt;tel++)
		{
			y_waarde[tel]=(int)(Max_abs-delta_y*tel);
			sprintf(ch,"%8.2g",y_waarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-95),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
		}
	}

	if (Max_abs < USHRT_MAX)
	{
		for(tel=0;tel < aantaltxt;tel++)
		{
			y_waarde[tel]=(Max_abs-delta_y*tel);
			sprintf(ch,"%5.1f",y_waarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-95),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
		}
	}

	for(tel=1;tel < aantaltxt-1;tel++)
		line(asm_draw_bitmap,LEFT_XOFFS+1,(int)-(tel*afstand-TOP_YOFFS),lengte+LEFT_XOFFS-1,(int)-(tel*afstand-TOP_YOFFS),
		    ASM_BLACK_COLOR);


}


/**********************************************************************************
 *                                                                                *
 * gen_freq_background genereerd het assenstelsel, met bijbehorende tekst, deze *
 * achtergrond verschild per domein                                               *
 *                                                                                *
 **********************************************************************************/

void gen_freq_background(VENSTER *vp)
{
	int tel;
	int lengte,hoogte,aantaltxt,sampler;
	double delta_f,delta_y,Max_abs;
	double eenheid,y_waarde[20],afstand;
	double *xwaarde;
	char ch[256],nm[40];
	ASM_SAMPLE *s;
	double Minimum, Maximum;               /* min en max waarden van record */


	strncpy(nm,"Signal ",10);
	strncat(nm,DataSignalName(vp),40);

	if (Window_Mode(vp)==REAL_M)
		strncat(nm,"  Real (Freq)",20);
	else
		strncat(nm,"  Imaginary (Freq)",20);

	lengte=Window_Width(vp) - LEFT_XOFFS - RIGHT_XOFFS;     /* Lengte horizontale as */
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;    /* Lengte verticale as */
	afstand= pixel_afst;                             /* Aantal pixels tussen twee getallen */
	sampler= DataSampleRate(vp)*10;                  /* Samplerate in Hz */

	textout_ex(asm_draw_bitmap,font,nm,(lengte/2),0,ASM_EMPH_COLOR, -1);

	aantaltxt= (int) ((lengte/afstand)+1);           /* Aantal getallen horizontale as */
	delta_f=(double)(sampler)/(double)(aantaltxt-1); /* freq tussen twee getallen */
	afstand=(double)(lengte)/(double)(aantaltxt-1);  /* Aantal pixels tussen twee getallen */

	eenheid=1e3;             /* Kies voor de eenheid 1000  (kHz)  */
	delta_f=delta_f/eenheid;

	sprintf(ch,"--> Frequency [kHz]");
	textout_ex(asm_draw_bitmap,font,ch,(lengte/2),maxy-text_height(font),ASM_GRAY_COLOR, -1);

	/* print 0  x-as  text*/
	sprintf(ch,"0");
	textout_ex(asm_draw_bitmap,font,ch,LEFT_XOFFS,maxy-28,ASM_GRAY_COLOR, -1);


	/* allocate data for xwaarde */
	if ((xwaarde = (double *) malloc(aantaltxt*(sizeof(double)))) == NULL)
	{
		asm_wprintf ("Error: malloc failed in gen_signal_background (xwaarde)");
	}
	else
	{
		for(tel=1;tel < aantaltxt;tel++)
		{
			xwaarde[tel]=(delta_f*tel);
			sprintf(ch,"%3.2f",xwaarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(int)(tel*afstand+LEFT_XOFFS-25),(int)(maxy-28),ASM_GRAY_COLOR, -1);
		}
		free(xwaarde);
	}

	/* print record_nr en channel_nr  */

	sprintf(ch,"Rec: %d - Chan: %d",Window_Record(vp),Window_Channel(vp));
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS+lengte-200),(TOP_YOFFS-10),ASM_GRAY_COLOR, -1);


	/* teken vertikaal grid  */

	for(tel=1;tel < aantaltxt;tel++)
		line(asm_draw_bitmap, LEFT_XOFFS+tel*afstand, TOP_YOFFS, LEFT_XOFFS+tel*afstand, hoogte+TOP_YOFFS, ASM_GRAY_COLOR);

	/* teken de x en y as in zwart (+ boven en rechts)   */

	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , LEFT_XOFFS        , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, lengte+LEFT_XOFFS, TOP_YOFFS          , lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , (maxy-BOTTEM_YOFFS), lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , lengte+LEFT_XOFFS , TOP_YOFFS         , ASM_BLACK_COLOR);



	/* Bepaal VSCALE m.b.v. Maximum en minimum     */



	if (Window_Mode(vp)==REAL_M)
	{
		s = DataReal(vp);     /* display real data */
	}

	else
	{
		s = DataImag(vp);     /* display imaginary data */
	}

	s = s + Window_Record(vp)*DataLength(vp);

	Minimum=Maximum=*s;                       /* Min en Max bepalen */

	for(tel=0;tel < (DataLength(vp));tel++)
	{
		if (*s > Maximum) Maximum=*s;
		if (*s < Minimum) Minimum=*s;
		s++;
	}


	Window_Max(vp)=Maximum;
	Window_Min(vp)=Minimum;

	aantaltxt=9;
	afstand = (int)(-hoogte/(aantaltxt-1));

	Max_abs = (double)MAX2(fabs(Maximum),fabs(Minimum)); /* Absolute maximum */
	if (Max_abs == 0.0) Max_abs = 1;
	delta_y = Max_abs*2/(aantaltxt-1);

	Window_VScale(vp) = Max_abs/(hoogte/2);

	if (Max_abs >= USHRT_MAX)
	{
		for(tel=0;tel < aantaltxt;tel++)
		{
			y_waarde[tel]=(int)(Max_abs-delta_y*tel);
			sprintf(ch,"%8.2g",y_waarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-95),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);

		}
	}

	if (Max_abs < USHRT_MAX)
	{
		for(tel=0;tel < aantaltxt;tel++)
		{
			y_waarde[tel]=(Max_abs-delta_y*tel);
			sprintf(ch,"%5.1f",y_waarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-95),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
		}
	}

	for(tel=1;tel < aantaltxt-1;tel++)

		line(asm_draw_bitmap,LEFT_XOFFS+1,(int)-(tel*afstand-TOP_YOFFS),lengte+LEFT_XOFFS-1,(int)-(tel*afstand-TOP_YOFFS),
		    ASM_BLACK_COLOR);

}

/***************************************************************************
 *
 * void gen_bode_background(VENSTER *vp)
 *
 ***************************************************************************/

void gen_bode_background(VENSTER *vp)
{
	int tel,midtype;
	int lengte,mag_hoogte,phase_hoogte,aantaltxt,sampler;
	int mag_top_offs;
	int delta_ph;
	double delta_f,delta_db,Max_abs;
	double eenheid,afstand;
	double *xwaarde;
	int ywaarde[100],ph_waarde[10];
	char ch[256],nm[40];
	ASM_SAMPLE *temp,*tempadres, *real_data, *imag_data;   /* pointer naar te displayen data */
	double Minimum, Maximum;                           /* min en max waarden van record */


	strncpy(nm,"Signal ",10);
	strncat(nm,DataSignalName(vp),40);
	strncat(nm,"  BodeDiagram",20);


	midtype=0;
	mag_hoogte = BODE_SIGNAL_HOOGTE;
	phase_hoogte = (int)(BODE_SIGNAL_HOOGTE/PH_MAG);
	mag_top_offs = phase_hoogte + 2*TOP_YOFFS;

	lengte=(Window_Width(vp) - LEFT_XOFFS - RIGHT_XOFFS);        /* Lengte horizontale as */
	/* Horizontale as + text */
	textout_ex(asm_draw_bitmap,font,nm,(lengte/2),0,ASM_EMPH_COLOR, -1);


	afstand= pixel_afst;                             /* Aantal pixels tussen twee getallen */
	sampler= DataSampleRate(vp)*10;                  /* Samplerate in Hz */

	aantaltxt= (int) ((lengte/afstand)+1);           /* Aantal getallen horizontale as */
	delta_f=(double)(sampler)/(double)(aantaltxt-1); /* freq tussen twee getallen */
	afstand=(double)(lengte)/(double)(aantaltxt-1);  /* Aantal pixels tussen twee getallen */

	eenheid=1e3;             /* Kies voor de eenheid 1000  (kHz)  */
	delta_f=delta_f/eenheid;

	sprintf(ch,"--> Frequency [kHz]");
	textout_ex(asm_draw_bitmap,font,ch,(lengte/2),maxy-text_height(font),ASM_GRAY_COLOR, -1);

	/* print 0  x-as  text*/
	sprintf(ch,"0");
	textout_ex(asm_draw_bitmap,font,ch,LEFT_XOFFS,maxy-28,ASM_GRAY_COLOR, -1);


	/* allocate data for xwaarde */
	if ((xwaarde = (double *) malloc(aantaltxt*(sizeof(double)))) == NULL)
	{
		asm_wprintf ("Error: malloc failed in gen_signal_background (xwaarde)");
	}
	else
	{
		for(tel=1;tel < aantaltxt;tel++)
		{
			xwaarde[tel]=(delta_f*tel);
			sprintf(ch,"%3.2f",xwaarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(int)(tel*afstand+LEFT_XOFFS-25),(int)(maxy-28),ASM_GRAY_COLOR, -1);
		}
		free(xwaarde);
	}


	/* print record_nr en channel_nr  */

	sprintf(ch,"          Chan: %d",Window_Channel(vp));
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS+lengte-200),(TOP_YOFFS-10),ASM_GRAY_COLOR, -1);


	/* teken vertikaal grid  */

	for(tel=1;tel < aantaltxt;tel++)
	{
		line(asm_draw_bitmap, LEFT_XOFFS+tel*afstand ,(TOP_YOFFS)    ,LEFT_XOFFS+tel*afstand ,(phase_hoogte+TOP_YOFFS)  ,
		    ASM_GRAY_COLOR);
		line(asm_draw_bitmap, LEFT_XOFFS+tel*afstand ,(mag_top_offs) ,LEFT_XOFFS+tel*afstand ,(mag_top_offs+mag_hoogte) ,
		    ASM_GRAY_COLOR);
	}




	/* teken de x en y as in zwart (+ boven en rechts)   */

	line(asm_draw_bitmap ,LEFT_XOFFS        ,mag_top_offs        ,LEFT_XOFFS        ,(maxy-BOTTEM_YOFFS) ,ASM_BLACK_COLOR);
	line(asm_draw_bitmap ,lengte+LEFT_XOFFS ,mag_top_offs        ,lengte+LEFT_XOFFS ,(maxy-BOTTEM_YOFFS) ,ASM_BLACK_COLOR);
	line(asm_draw_bitmap ,LEFT_XOFFS        ,(maxy-BOTTEM_YOFFS) ,lengte+LEFT_XOFFS ,(maxy-BOTTEM_YOFFS) ,ASM_BLACK_COLOR);
	line(asm_draw_bitmap ,LEFT_XOFFS        ,mag_top_offs        ,lengte+LEFT_XOFFS ,mag_top_offs        ,ASM_BLACK_COLOR);

	line(asm_draw_bitmap ,LEFT_XOFFS        ,TOP_YOFFS              ,LEFT_XOFFS       , TOP_YOFFS+phase_hoogte, ASM_BLACK_COLOR);
	line(asm_draw_bitmap ,lengte+LEFT_XOFFS ,TOP_YOFFS              ,lengte+LEFT_XOFFS, TOP_YOFFS+phase_hoogte, ASM_BLACK_COLOR);
	line(asm_draw_bitmap ,LEFT_XOFFS        ,TOP_YOFFS              ,lengte+LEFT_XOFFS, TOP_YOFFS             , ASM_BLACK_COLOR);
	line(asm_draw_bitmap ,LEFT_XOFFS        ,TOP_YOFFS+phase_hoogte ,lengte+LEFT_XOFFS, TOP_YOFFS+phase_hoogte, ASM_BLACK_COLOR);

	/* Bepaal VSCALE m.b.v. Maximum en minimum voor de phase    */


	Window_Max(vp)=PHASE_MAX;
	Window_Min(vp)=PHASE_MIN;

	Max_abs = Window_Max(vp)-Window_Min(vp); /* verschil tussen minimum en maximum */
	aantaltxt = 5;
	afstand = (int)(-phase_hoogte/(aantaltxt-1));
	delta_ph = (int)(Max_abs/(aantaltxt-1));                     /* delta is 20 graden */

	Window_VScale(vp) = Max_abs/(double)(phase_hoogte);
	/*  asm_wprintf("MA: %g,phase_hoogte %d, Window_V: %g\n",Max_abs,phase_hoogte,Window_VScale(vp)); */

	for(tel=0;tel < aantaltxt;tel++)
	{
		ph_waarde[tel]=(int)(Window_Max(vp)-delta_ph*tel);
		sprintf(ch,"%7d",ph_waarde[tel]);
		textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-80),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
	}

	sprintf(ch,"Degrees");
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS),(TOP_YOFFS-10),ASM_GRAY_COLOR, -1);

	for(tel=1;tel < aantaltxt-1;tel++)
	{
		line(asm_draw_bitmap ,LEFT_XOFFS+1 ,-(tel*afstand-TOP_YOFFS) ,lengte+LEFT_XOFFS-1, -(tel*afstand-TOP_YOFFS) ,
		    ASM_BLACK_COLOR);
	}


	/* Bepaal VSCALE m.b.v. Maximum en minimum voor de magnitude    */


	temp = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (temp == NULL)
		asm_wprintf("Error in gen_bode_background: malloc failed");

	tempadres=temp;
	real_data = DataReal(vp);     /* display real data */
	imag_data = DataImag(vp);     /* display imaginary data */

	for(tel=0;tel < (DataLength(vp));tel++)
	{
		temp[tel] = 10*log10(sqrt(pow(real_data[tel],2) + pow(imag_data[tel],2)));
	}

	Minimum=Maximum=*temp;                       /* Min en Max bepalen */

	for(tel=0;tel < (DataLength(vp));tel++)
	{
		if (*temp > Maximum) Maximum=*temp;
		if (*temp < Minimum) Minimum=*temp;
		temp++;
	}
	temp=tempadres;
	free(temp);

	Window_Max(vp)= ((int)(Maximum/10)+1) * 10;
	Window_Min(vp)= ((int)(Minimum/10)-1) * 10;

	Max_abs = Window_Max(vp)-Window_Min(vp); /* verschil tussen minimum en maximum */
	aantaltxt = (int)(Max_abs/10) + 1;
	afstand = -mag_hoogte/(aantaltxt-1);
	delta_db = 10;

	Window_VScale(vp) = Max_abs/(mag_hoogte);

	for(tel=0;tel < aantaltxt;tel++)
	{
		ywaarde[tel]=(int)(Window_Max(vp)-delta_db*tel);
		sprintf(ch,"%7d",ywaarde[tel]);
		textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-80),(int)-(tel*afstand-mag_top_offs-5),ASM_GRAY_COLOR, -1);
	}

	sprintf(ch,"dB");
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS),(mag_top_offs-10),ASM_GRAY_COLOR, -1);

	for(tel=1;tel < aantaltxt-1;tel++)
	{
		line(asm_draw_bitmap ,LEFT_XOFFS+1 ,-(tel*afstand-mag_top_offs) ,lengte+LEFT_XOFFS-1, -(tel*afstand-mag_top_offs) ,
		    ASM_BLACK_COLOR);
	}


}


/***************************************************************************
 *
 * void gen_magnitude_background(VENSTER *vp)
 *
 ***************************************************************************/

void gen_magnitude_background(VENSTER *vp)
{
	int tel;
	int lengte,hoogte,aantaltxt,sampler;
	int ywaarde[100];
	double delta_f,delta_db,Max_abs;
	double eenheid,afstand;
	double *xwaarde;
	char ch[256],nm[40];
	ASM_SAMPLE *temp,*tempadres,*real_data, *imag_data;                            /* pointer naar te displayen data */
	double Minimum, Maximum;               /* min en max waarden van record */

	strncpy(nm,"Signal ",10);
	strncat(nm,DataSignalName(vp),40);
	strncat(nm,"  Magnitude (Freq)",20);


	lengte=Window_Width(vp) - LEFT_XOFFS - RIGHT_XOFFS;     /* Lengte horizontale as */
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;    /* Lengte verticale as */

	textout_ex(asm_draw_bitmap,font,nm,(lengte/2),0,ASM_EMPH_COLOR, -1);

	afstand= pixel_afst;                             /* Aantal pixels tussen twee getallen */
	sampler= DataSampleRate(vp)*10;                  /* Samplerate in Hz */

	aantaltxt= (int) ((lengte/afstand)+1);           /* Aantal getallen horizontale as */
	delta_f=(double)(sampler)/(double)(aantaltxt-1); /* freq tussen twee getallen */
	afstand=(double)(lengte)/(double)(aantaltxt-1);  /* Aantal pixels tussen twee getallen */

	eenheid=1e3;             /* Kies voor de eenheid 1000  (kHz)  */
	delta_f=delta_f/eenheid;

	sprintf(ch,"--> Frequency [kHz]");
	textout_ex(asm_draw_bitmap,font,ch,(lengte/2),maxy-text_height(font),ASM_GRAY_COLOR, -1);

	/* print 0  x-as  text*/
	sprintf(ch,"0");
	textout_ex(asm_draw_bitmap,font,ch,LEFT_XOFFS,maxy-28,ASM_GRAY_COLOR, -1);


	/* allocate data for xwaarde */
	if ((xwaarde = (double *) malloc(aantaltxt*(sizeof(double)))) == NULL)
	{
		asm_wprintf ("Error: malloc failed in gen_signal_background (xwaarde)");
	}
	else
	{
		for(tel=1;tel < aantaltxt;tel++)
		{
			xwaarde[tel]=(delta_f*tel);
			sprintf(ch,"%3.2f",xwaarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(int)(tel*afstand+LEFT_XOFFS-25),(int)(maxy-28),ASM_GRAY_COLOR, -1);
		}
		free(xwaarde);
	}


	/* print record_nr en channel_nr  */

	sprintf(ch,"          Chan: %d",Window_Channel(vp));
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS+lengte-200),(TOP_YOFFS-10),ASM_GRAY_COLOR, -1);


	/* teken vertikaal grid  */

	for(tel=1;tel < aantaltxt;tel++)
		line(asm_draw_bitmap, LEFT_XOFFS+tel*afstand, TOP_YOFFS, LEFT_XOFFS+tel*afstand, hoogte+TOP_YOFFS, ASM_GRAY_COLOR);


	/* teken de x en y as in zwart (+ boven en rechts)   */

	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , LEFT_XOFFS        , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, lengte+LEFT_XOFFS, TOP_YOFFS          , lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , (maxy-BOTTEM_YOFFS), lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , lengte+LEFT_XOFFS , TOP_YOFFS         , ASM_BLACK_COLOR);

	/* Bepaal VSCALE m.b.v. Maximum en minimum     */


	temp = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (temp == NULL)
	{
		asm_wprintf("Error in gen_magnitude_background: malloc failed\r");
		return;
	}

	tempadres=temp;
	real_data = DataReal(vp);     /* display real data */
	imag_data = DataImag(vp);     /* display imaginary data */

	if (Window_Log(vp))
		for(tel=0;tel < (DataLength(vp));tel++)
		{
			temp[tel] = 10*log10(sqrt(pow(real_data[tel],2) + pow(imag_data[tel],2)));
		}
	else
		for(tel=0;tel < (DataLength(vp));tel++)
		{
			temp[tel] = (sqrt(pow(real_data[tel],2) + pow(imag_data[tel],2)));
		}

	Minimum=Maximum=*temp;                       /* Min en Max bepalen */

	for(tel=0;tel < (DataLength(vp));tel++)
	{
		if (*temp > Maximum) Maximum=*temp;
		if (*temp < Minimum) Minimum=*temp;
		temp++;
	}
	temp=tempadres;
	free(temp);

	if (Window_Log(vp))
	{
		Window_Max(vp)= ((int)(Maximum/10)+1) * 10;
		Window_Min(vp)= ((int)(Minimum/10)-1) * 10;
		Max_abs = Window_Max(vp)-Window_Min(vp); /* verschil tussen minimum en maximum */
		aantaltxt = (int)(Max_abs/10) + 1;
		delta_db=10;
	}
	else
	{
		Window_Max(vp)= Maximum;
		Window_Min(vp)= 0;
		if (Window_Max(vp) <=1) Window_Max(vp)=1;
		Max_abs = Window_Max(vp);
		aantaltxt=9;
		delta_db= Max_abs/(aantaltxt-1);
	}

	afstand = -hoogte/(aantaltxt-1);

	Window_VScale(vp) = Max_abs/(hoogte);

	for(tel=0;tel < aantaltxt;tel++)
	{
		ywaarde[tel]=(int)(Window_Max(vp)-delta_db*tel);
		sprintf(ch,"%7d",ywaarde[tel]);
		textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-80),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
	}

	if (Window_Log(vp))
	{
		sprintf(ch,"dB");
		textout_ex(asm_draw_bitmap, font, ch, LEFT_XOFFS, TOP_YOFFS-10, ASM_GRAY_COLOR, -1);
	}

	for(tel=1;tel < aantaltxt-1;tel++)
	{
		line(asm_draw_bitmap, LEFT_XOFFS+1 , -(tel*afstand-TOP_YOFFS) , lengte+LEFT_XOFFS-1 , -(tel*afstand-TOP_YOFFS) ,
		    ASM_GRAY_COLOR);
	}

}

/*************************************************************************
 *
 * void gen_phase_background(VENSTER *vp)
 *
 *************************************************************************/


void gen_phase_background(VENSTER *vp)
{
	int tel;
	int lengte,hoogte,aantaltxt,sampler;
	double delta,delta_f,Max_abs;
	double eenheid,afstand;
	double *xwaarde;
	int ywaarde[100];
	char ch[256],nm[40];


	strncpy(nm,"Signal ",10);
	strncat(nm,DataSignalName(vp),40);
	strncat(nm,"  Phase",20);

	Window_Width(vp)=Window_HScale(vp)*DataLength(vp) + LEFT_XOFFS + RIGHT_XOFFS;
	Window_Height(vp)= (SIGNAL_HOOGTE + TOP_YOFFS + BOTTEM_YOFFS);

	lengte=Window_Width(vp) - LEFT_XOFFS - RIGHT_XOFFS;     /* Lengte horizontale as */
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;    /* Lengte verticale as */
	afstand= pixel_afst;                             /* Aantal pixels tussen twee getallen */
	sampler= DataSampleRate(vp)*10;                  /* Samplerate in Hz */

	textout_ex(asm_draw_bitmap,font,nm,(lengte/2),0,ASM_EMPH_COLOR, -1);

	aantaltxt= (int) ((lengte/afstand)+1);           /* Aantal getallen horizontale as */
	delta_f=(double)(sampler)/(double)(aantaltxt-1); /* freq tussen twee getallen */
	afstand=(double)(lengte)/(double)(aantaltxt-1);  /* Aantal pixels tussen twee getallen */

	eenheid=1e3;             /* Kies voor de eenheid 1000  (kHz)  */
	delta_f=delta_f/eenheid;

	sprintf(ch,"--> Frequency [kHz]");
	textout_ex(asm_draw_bitmap,font,ch,(lengte/2),maxy-text_height(font),ASM_GRAY_COLOR, -1);

	/* print 0  x-as  text*/
	sprintf(ch,"0");
	textout_ex(asm_draw_bitmap,font,ch,LEFT_XOFFS,maxy-28,ASM_GRAY_COLOR, -1);


	/* allocate data for xwaarde */
	if ((xwaarde = (double *) malloc(aantaltxt*(sizeof(double)))) == NULL)
	{
		asm_wprintf ("Error: malloc failed in gen_signal_background (xwaarde)");
	}
	else
	{
		for(tel=1;tel < aantaltxt;tel++)
		{
			xwaarde[tel]=(delta_f*tel);
			sprintf(ch,"%3.2f",xwaarde[tel]);
			textout_ex(asm_draw_bitmap,font,ch,(int)(tel*afstand+LEFT_XOFFS-25),(int)(maxy-28),ASM_GRAY_COLOR, -1);
		}
		free(xwaarde);
	}


	/* print record_nr en channel_nr  */

	sprintf(ch,"          Chan: %d",Window_Channel(vp));
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS+lengte-200),(TOP_YOFFS-10),ASM_GRAY_COLOR, -1);

	/* teken vertikaal grid  */

	for(tel=1;tel < aantaltxt;tel++)
		line(asm_draw_bitmap, LEFT_XOFFS+tel*afstand, TOP_YOFFS, LEFT_XOFFS+tel*afstand, hoogte+TOP_YOFFS, ASM_GRAY_COLOR);


	/* teken de x en y as in zwart (+ boven en rechts)   */

	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , LEFT_XOFFS        , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, lengte+LEFT_XOFFS, TOP_YOFFS          , lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , (maxy-BOTTEM_YOFFS), lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , lengte+LEFT_XOFFS , TOP_YOFFS         , ASM_BLACK_COLOR);


	/* Bepaal VSCALE m.b.v. Maximum en minimum     */

	Window_Max(vp)=PHASE_MAX;
	Window_Min(vp)=PHASE_MIN;

	Max_abs = Window_Max(vp)-Window_Min(vp); /* verschil tussen minimum en maximum */
	aantaltxt = 11;
	afstand = -hoogte/(aantaltxt-1);
	delta = Max_abs/(aantaltxt-1);                     /* delta is 20 graden */

	Window_VScale(vp) = Max_abs/(hoogte);

	for(tel=0;tel < aantaltxt;tel++)
	{
		ywaarde[tel]=(int)(Window_Max(vp)-delta*tel);
		sprintf(ch,"%7d",ywaarde[tel]);
		textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-80),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
	}

	sprintf(ch,"Degrees");
	textout_ex(asm_draw_bitmap, font, ch, LEFT_XOFFS, TOP_YOFFS-10, ASM_GRAY_COLOR, -1);

	for(tel=1;tel < aantaltxt-1;tel++)
	{
		line(asm_draw_bitmap, LEFT_XOFFS+1, -(tel*afstand-TOP_YOFFS), lengte+LEFT_XOFFS-1, -(tel*afstand-TOP_YOFFS),
		    ASM_GRAY_COLOR);
	}

}

/*************************************************************************
 *
 * void gen_hist_background(VENSTER *vp)
 *
 *************************************************************************/

void gen_hist_background(VENSTER *vp)
{
	int tel;
	int lengte,hoogte,aantaltxt;
	int ywaarde[100];
	double delta_a,delta_n,Max_abs;
	double xwaarde, afstand;
	char ch[256],nm[40];
	ASM_SAMPLE *real_data;   /* pointer naar te displayen data */

	strncpy(nm,"Signal ",10);
	strncat(nm,DataSignalName(vp),40);
	strncat(nm,"  Histogram (Time)",20);

	Window_Width(vp)=Window_HScale(vp)*DataLength(vp) + LEFT_XOFFS + RIGHT_XOFFS;
	Window_Height(vp)= (SIGNAL_HOOGTE + TOP_YOFFS + BOTTEM_YOFFS);

	lengte  = Window_Width(vp) - LEFT_XOFFS - RIGHT_XOFFS;     /* Lengte horizontale as */
	hoogte  = Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;    /* Lengte verticale as */
	afstand = pixel_afst;                             /* Aantal pixels tussen twee getallen */

	textout_ex(asm_draw_bitmap,font,nm,(lengte/2),0,ASM_EMPH_COLOR, -1);

	aantaltxt = (int) ((lengte/afstand)+1);           /* Aantal getallen horizontale as */
	delta_a   = (double)(DataRealMax(vp)-DataRealMin(vp))/(double)(aantaltxt-1); /* ampl tussen twee getallen */
	afstand   = (double)(lengte)/(double)(aantaltxt-1);  /* Aantal pixels tussen twee getallen */

	sprintf(ch,"--> Magnitude");
	textout_ex(asm_draw_bitmap,font,ch,(lengte/2),maxy-text_height(font),ASM_GRAY_COLOR, -1);

	/* print 0  x-as  text*/
	sprintf(ch,"%5.1f",DataRealMin(vp));
	textout_ex(asm_draw_bitmap,font,ch,LEFT_XOFFS,maxy-28,ASM_GRAY_COLOR, -1);


	/*  for(tel=1;tel < aantaltxt;tel++)
   {
    */
	tel = aantaltxt -1;

	/*  xwaarde[tel]=(delta_a*tel+DataRealMin(vp));
    sprintf(ch,"%5.1f",xwaarde[tel]);             */

	xwaarde=(delta_a*tel+DataRealMin(vp));
	sprintf(ch,"%5.1f",xwaarde);
	textout_ex(asm_draw_bitmap,font,ch,(int)(tel*afstand+LEFT_XOFFS-25),(int)(maxy-28),ASM_GRAY_COLOR, -1);

	/*   }   */



	/* print record_nr en channel_nr  */

	sprintf(ch,"Chan: %d",Window_Channel(vp));
	textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS+lengte-100),TOP_YOFFS-10,ASM_GRAY_COLOR, -1);


	/* teken vertikaal grid  */

	for(tel=1;tel < aantaltxt;tel++)
		line(asm_draw_bitmap, LEFT_XOFFS+tel*afstand, TOP_YOFFS, LEFT_XOFFS+tel*afstand, hoogte+TOP_YOFFS, ASM_GRAY_COLOR);


	/* teken de x en y as in zwart (+ boven en rechts)   */

	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , LEFT_XOFFS        , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, lengte+LEFT_XOFFS, TOP_YOFFS          , lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , (maxy-BOTTEM_YOFFS), lengte+LEFT_XOFFS , maxy-BOTTEM_YOFFS , ASM_BLACK_COLOR);
	line(asm_draw_bitmap, LEFT_XOFFS       , TOP_YOFFS          , lengte+LEFT_XOFFS , TOP_YOFFS         , ASM_BLACK_COLOR);


	/* Bepaal VSCALE m.b.v. Maximum en minimum     */


	real_data = DataReal(vp);     /* display real data */

	Window_Min(vp) = 0.0;
	Window_Max(vp) = *real_data;                       /* Min en Max bepalen */

	for(tel=0;tel < (DataLength(vp));tel++)
	{
		if (*real_data > Window_Max(vp)) Window_Max(vp)=*real_data;
		real_data++;
	}
	real_data = DataReal(vp);

	Max_abs = 100; /* verschil tussen minimum en maximum in %  */
	aantaltxt = (int)11;
	afstand = -hoogte/(aantaltxt-1);
	delta_n = 10;

	Window_VScale(vp) = Window_Max(vp)/(hoogte);

	for(tel=0;tel < aantaltxt;tel++)
	{
		ywaarde[tel]=(int)(100-delta_n*tel);
		sprintf(ch,"%7d",ywaarde[tel]);
		textout_ex(asm_draw_bitmap,font,ch,(LEFT_XOFFS-80),(int)-(tel*afstand-TOP_YOFFS-5),ASM_GRAY_COLOR, -1);
	}

	sprintf(ch,"%%");
	textout_ex(asm_draw_bitmap, font, ch, LEFT_XOFFS, TOP_YOFFS-10, ASM_GRAY_COLOR, -1);

	for(tel=1;tel < aantaltxt-1;tel++)
	{
		line(asm_draw_bitmap, LEFT_XOFFS+1, -(tel*afstand-TOP_YOFFS), lengte+LEFT_XOFFS-1, -(tel*afstand-TOP_YOFFS),
		    ASM_GRAY_COLOR);
	}

}

/**************************************************************************
 *
 * void gen_hist_plot(VENSTER *vp)
 *
 **************************************************************************/

void gen_hist_plot(VENSTER *vp)
{
	int   i,nul_offs,hoogte;
	double vscale,hscale,cscale;
	ASM_SAMPLE *s;                         /* pointer naar te displayen data */
	ASM_SAMPLE *temp,*cum,*tempadres;      /* tijdelijke copy voor scalen */



	vscale=Window_VScale(vp);
	hscale=Window_HScale(vp);
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;


	temp = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (temp == NULL)
	{
		asm_wprintf("Error in gen_hist_plot: malloc failed (temp)");
		return;
	}

	cum = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (cum == NULL)
	{
		asm_wprintf("Error in gen_hist_plot: malloc failed (cum)");
		free(temp);
		return;
	}


	tempadres = temp;
	s = DataReal(vp);     /* display real data */

	/* Welk record displayen? */

	s = s + (Window_Channel(vp)*DataRecords(vp) + Window_Record(vp))*DataLength(vp);

	for(i=0;i < (DataLength(vp));i++)
	{
		*temp=*s;
		temp++;
		s++;
	}

	temp = tempadres;


	cum[0]=temp[0];

	for(i=0; i < (DataLength(vp));i++)
	{
		if (i > 0)
			cum[i]=cum[i-1]+temp[i];
		temp[i]= (temp[i]/vscale);
	}

	cscale=cum[DataLength(vp)-1]/hoogte;

	nul_offs = hoogte+TOP_YOFFS;

	for(i=0;i < (DataLength(vp));i++)
	{
		line(asm_draw_bitmap, (int)(hscale*i+LEFT_XOFFS), nul_offs, (int)(hscale*i+LEFT_XOFFS), (int)-(temp[i]-nul_offs),
		    ASM_BLUE_COLOR);
	}

	for(i=0;i < (DataLength(vp)-1);i++)
	{
		line(asm_draw_bitmap, (int)(hscale*i+LEFT_XOFFS), (int)-((cum[i]/cscale)-nul_offs), (int)(hscale*(i+1) +
		    LEFT_XOFFS), (int)-((cum[i+1]/cscale)-nul_offs), ASM_GREEN_COLOR);
	}


	free(cum);
	free(temp);

}


/**************************************************************************************/
/* Genereer het DRAW-Object voor het te plotten signaal                               */
/**************************************************************************************/


void gen_signal_plot(VENSTER *vp)
{
	int   i,nul_offs,hoogte,x1, x2, y1, y2;
	double vscale,hscale;
	ASM_SAMPLE *s;                     /* pointer naar te displayen data */
	ASM_SAMPLE *temp,*tempadres;                  /* tijdelijke copy voor scalen */



	vscale=Window_VScale(vp);
	hscale=Window_HScale(vp);
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;


	temp = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (temp == NULL)
		asm_wprintf("Error in gen_signal_plot: malloc failed");

	tempadres = temp;

	s = DataReal(vp);     /* display real data */

	if (Window_Mode(vp)==IMAG_M)
	{
		s = DataImag(vp);     /* display imaginary data */
	}

	/* Welk record displayen? */

	s = s + (Window_Channel(vp)*DataRecords(vp) + Window_Record(vp))*DataLength(vp);

	/*   asm_wprintf("Signal %s, Channel %d, Record %d\n",DataSignalName(vp),Window_Channel(vp),Window_Record(vp)); */

	for(i=0;i < (DataLength(vp));i++)
	{
		*temp=*s;
		temp++;
		s++;
	}

	temp = tempadres;

	for(i=0; i < (DataLength(vp));i++)
	{
		temp[i]= (temp[i]/vscale);
	}

	switch (DataDomain(vp))
	{
	case TIME:
		nul_offs = hoogte/2+TOP_YOFFS;

		if (!bon)
		{
			for(i=0;i < (DataLength(vp)-1);i++)
			{
				x1= (int)(hscale*i+LEFT_XOFFS);
				x2= (int)(hscale*(i+1) + LEFT_XOFFS);
				y1= (int)-(temp[i]-nul_offs);
				y2= (int)-(temp[i+1]-nul_offs);
				/*	  asm_wprintf(" %d %d %d %d",x1,y1,x2,y2);  */
				line(asm_draw_bitmap,x1, y1, x2  , y2 , ASM_RED_COLOR);
			}
		}
		else
		{
			for(i=0;i < (DataLength(vp));i++)
			{
				line(asm_draw_bitmap,(int)(hscale*i+LEFT_XOFFS),(int)(nul_offs) ,(int)(hscale*i+LEFT_XOFFS),
				    (int)-(temp[i]-nul_offs),ASM_RED_COLOR);
			}
		}
		break;

	case FREQ:
		nul_offs = TOP_YOFFS+(Window_Max(vp)/Window_VScale(vp));

		if (!bon)
		{
			for(i=0;i < (DataLength(vp)-1);i++)
			{
				x1= (int)(hscale*i+LEFT_XOFFS);
				x2= (int)(hscale*(i+1) + LEFT_XOFFS);
				line(asm_draw_bitmap,x1,(int)-(temp[i]-nul_offs) ,x2  ,(int)-(temp[i+1]-nul_offs),ASM_GREEN_COLOR);
			}
		}
		else
			for(i=0;i < (DataLength(vp));i++)
			{
				line(asm_draw_bitmap,(int)(hscale*i+LEFT_XOFFS),(int)(nul_offs) ,(int)(hscale*i+LEFT_XOFFS),
				    (int)-(temp[i]-nul_offs),ASM_GREEN_COLOR);
			}

		break;
	case MAGN:
		nul_offs = hoogte/2+TOP_YOFFS;

		if(!bon)
		{
			for(i=0;i < (DataLength(vp)-1);i++)
			{
				line(asm_draw_bitmap,(int)(hscale*i+LEFT_XOFFS),(int)-(temp[i]-nul_offs) ,(int)(hscale*(i+1) +
				    LEFT_XOFFS),(int)-(temp[i+1]-nul_offs),ASM_RED_COLOR);
			}
		}
		else
		{
			for(i=0;i < (DataLength(vp));i++)
			{
				line(asm_draw_bitmap,(int)(hscale*i+LEFT_XOFFS),(int)(nul_offs) , (int)(hscale*i+LEFT_XOFFS),
				    (int)-(temp[i]-nul_offs),ASM_RED_COLOR);
			}
		}
		break;
	default:
		break;
	}

	free(temp);

}

/***************************************************************************
 *
 * gen_bode_plot(VENSTER *vp,int mid_type)
 * plot de signalen voor het bodediagram
 *
 ***************************************************************************/

void gen_bode_plot(VENSTER *vp,int mid_type)
{
	int   i,j,nul_offs,hoogte, mag_hoogte,phase_hoogte,mag_top_offs,Max_abs;
	int x1, y1, x2, y2;
	unsigned int data_nr, offs;
	double vscale,hscale;
	ASM_SAMPLE *real_data, *imag_data;       /* pointer naar te displayen data */
	ASM_SAMPLE *temp, *cum1, *cum2;          /* tijdelijke copy voor scalen */

	vscale=Window_VScale(vp);
	hscale=Window_HScale(vp);
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;


	mag_hoogte = BODE_SIGNAL_HOOGTE;
	phase_hoogte = (int)(BODE_SIGNAL_HOOGTE/PH_MAG);
	mag_top_offs = phase_hoogte + 2*TOP_YOFFS;


	temp = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (temp == NULL)
	{
		asm_wprintf("Error in gen_bode_plot: malloc failed (temp)");
		return;
	}

	cum1 = (ASM_SAMPLE *) calloc((unsigned int)DataLength(vp),sizeof(ASM_SAMPLE));
	if (cum1 == NULL)
	{
		asm_wprintf("Error in gen_bode_plot: calloc failed (cum1)");
		free(temp);
		return;
	}

	cum2 = (ASM_SAMPLE *) calloc((unsigned int)DataLength(vp),sizeof(ASM_SAMPLE));
	if (cum2 == NULL)
	{
		asm_wprintf("Error in gen_bode_plot: calloc failed (cum2)");
		free(cum1);
		free(temp);
		return;
	}

	/* genereer magnitude gedeelte */

	real_data = DataReal(vp);     /* display real data */
	imag_data = DataImag(vp);     /* display imaginary data */

	/* Bereken de offset van het te displayen channel */

	offs = Window_Channel(vp)*DataRecords(vp)*DataLength(vp);

	/*  asm_wprintf("Signal %s, Channel %d\n",DataSignalName(vp),Window_Channel(vp)); */

	if (mid_type == 0)
	{
		for (j=0; j < DataRecords(vp);j++)
		{
			for (i=0;i < DataLength(vp);i++)
			{
				data_nr = offs+i+j*DataLength(vp);
				/* Bepaal de magnitude */
				temp[i] = sqrt(pow(real_data[data_nr],2) + pow(imag_data[data_nr],2));
				/* Tel de magnitudes bij elkaar op */
				cum1[i] = cum1[i] + temp[i];
			}
		}

		for (i=0;i < DataLength(vp);i++)
			cum1[i]=10.0*log10(cum1[i]/DataRecords(vp)) / Window_VScale(vp);

	}

	if (mid_type == 1)
	{
		for (j=0; j < DataRecords(vp);j++)
			for (i=0;i < DataLength(vp);i++)
			{
				/* Tel de records bij elkaar op */
				cum1[i] = cum1[i] + (real_data[offs + i+j*DataLength(vp)]);
				cum2[i] = cum2[i] + (imag_data[offs + i+j*DataLength(vp)]);
			}
		for (i=0;i < DataLength(vp);i++)
		{
			/* Deel door het aantal records */
			cum1[i]=cum1[i]/(double)(DataRecords(vp));
			cum2[i]=cum2[i]/(double)(DataRecords(vp));
			cum1[i]=10*log10( sqrt( pow(cum1[i],2) + pow(cum2[i],2))) / Window_VScale(vp);
		}

	}

	nul_offs = mag_top_offs+(Window_Max(vp)/Window_VScale(vp));

	if (!bon)
	{
		for(i=0;i < (DataLength(vp)-1);i++)
		{
			x1 = (int)(hscale*i+LEFT_XOFFS);
			y1 = (int)-(cum1[i]-nul_offs);
			x2 = (int)(hscale*(i+1)+LEFT_XOFFS);
			y2 = (int)-(cum1[i+1]-nul_offs);
			line(asm_draw_bitmap , x1 ,y1 ,x2 , y2, ASM_GREEN_COLOR);
		}
	}
	else
	{
		for(i=0;i < (DataLength(vp));i++)
		{
			line(asm_draw_bitmap , (int)(hscale*i+LEFT_XOFFS) ,(int)(hoogte+TOP_YOFFS) ,(int)(hscale*i+LEFT_XOFFS) ,
			    (int)-(cum1[i]-nul_offs),ASM_GREEN_COLOR);
		}
	}

	/* genereer phase gedeelte  */

	Window_Max(vp)=PHASE_MAX;
	Window_Min(vp)=PHASE_MIN;

	Max_abs = Window_Max(vp)-Window_Min(vp); /* verschil tussen minimum en maximum */
	Window_VScale(vp) = Max_abs/(double)(phase_hoogte);


	/*
asm_wprintf("Max: %d , Window_VS(vp): %f , ph_h %d \n",Max_abs,Window_VScale(vp),phase_hoogte);
*/
	for (i=0;i < DataLength(vp);i++) cum1[i] = 0;

	if (mid_type == 0)
	{
		for (j=0; j < DataRecords(vp);j++)

		{
			for (i=0;i < DataLength(vp);i++)
			{
				data_nr = offs+i+j*DataLength(vp);
				temp[i] = (180/PI)*atan2(imag_data[data_nr],real_data[data_nr]);
				cum1[i] = cum1[i] + temp[i];
			}
		}

		for (i=0;i < DataLength(vp);i++)
			cum1[i]=(cum1[i]/DataRecords(vp))/Window_VScale(vp);


	}
	if (mid_type == 1)
	{
		for (j=0; j < DataRecords(vp);j++)
			for (i=0;i < DataLength(vp);i++)
			{
				temp[i] = (real_data[offs + i+j*DataLength(vp)]);
				cum1[i] = cum1[i] + temp[i];
				temp[i] = (imag_data[offs + i+j*DataLength(vp)]);
				cum2[i] = cum2[i] + temp[i];
			}
		for (i=0;i < DataLength(vp);i++)
		{
			cum1[i] = cum1[i]/DataRecords(vp);
			cum2[i] = cum2[i]/DataRecords(vp);

			cum1[i]= (180/PI)*atan2(cum2[i],cum1[i])/Window_VScale(vp);
		}

	}


	nul_offs = TOP_YOFFS+(Window_Max(vp)/Window_VScale(vp));

	if(!bon)
	{
		for(i=0;i < (DataLength(vp)-1);i++)
		{
			x1 = (int)(hscale*i+LEFT_XOFFS);
			y1 = (int)-(cum1[i]-nul_offs);
			x2 = (int)(hscale*(i+1) + LEFT_XOFFS);
			y2 = (int)-(cum1[i+1]-nul_offs);
			line(asm_draw_bitmap , x1 ,y1 ,x2 , y2, ASM_GREEN_COLOR);
		}
	}
	else
	{
		for(i=0;i < (DataLength(vp));i++)
		{
			x1 = (int)(hscale*i+LEFT_XOFFS);
			y1 = (int)(nul_offs);
			x2 = (int)(hscale*i + LEFT_XOFFS);
			y2 = (int)-(cum1[i] - nul_offs);
			line(asm_draw_bitmap , x1 ,y1 ,x2 , y2, ASM_GREEN_COLOR);
		}
	}


	free(cum2);
	free(cum1);
	free(temp);


}

/**************************************************************************
 *
 * void gen_magnitude_plot(VENSTER *vp,int mid_type)
 *
 **************************************************************************/

void gen_magnitude_plot(VENSTER *vp,int mid_type)
{
	int   i,nul_offs,hoogte;
	double vscale,hscale;
	ASM_SAMPLE *real_data;             /* pointer naar te displayen data */
	ASM_SAMPLE *temp;                  /* tijdelijke copy voor scalen */

	vscale=Window_VScale(vp);
	hscale=Window_HScale(vp);
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;

	temp = (ASM_SAMPLE *) malloc((unsigned int)DataLength(vp)*sizeof(ASM_SAMPLE));
	if (temp == NULL)
	{
		asm_wprintf("Error in gen_magnitude_plot: malloc failed (temp)");
		return;
	}

	real_data = DataReal(vp);     /* display real data */

	/* Bereken het adres van het channel dat je wil zien. */

	real_data = real_data + (Window_Channel(vp)*DataRecords(vp)+Window_Record(vp))*DataLength(vp);

	/*  asm_wprintf("Signal %s, Channel %d\n",DataSignalName(vp),Window_Channel(vp)); */

	if (Window_Log(vp))
	{
		for (i=0;i < DataLength(vp);i++)
			temp[i]=(10*log10(real_data[i]))/Window_VScale(vp);
	}
	else
	{
		for (i=0;i < DataLength(vp);i++)
			temp[i]=((real_data[i]))/Window_VScale(vp);
	}

	nul_offs = TOP_YOFFS+(Window_Max(vp)/Window_VScale(vp));

	if (!bon)
	{
		for(i=0;i < (DataLength(vp)-1);i++)
		{
			line(asm_draw_bitmap , (int)(hscale*i+LEFT_XOFFS) , (int)-(temp[i]-nul_offs), (int)(hscale*(i+1) +
			    LEFT_XOFFS), (int)-(temp[i+1]-nul_offs), ASM_GREEN_COLOR);
		}
	}
	else
	{
		for(i=0;i < (DataLength(vp));i++)
		{
			line(asm_draw_bitmap , (int)(hscale*i+LEFT_XOFFS) , (int)(hoogte+TOP_YOFFS), (int)(hscale*i+LEFT_XOFFS),
			    (int)-(temp[i]-nul_offs), ASM_GREEN_COLOR);
	}
}


free(temp);
}

/**************************************************************************
 *
 * void gen_phase_plot(VENSTER *vp,int mid_type)
 *
 **************************************************************************/

void gen_phase_plot(VENSTER *vp,int mid_type)
{
	int   i,nul_offs,hoogte;
	double vscale,hscale;
	ASM_SAMPLE *real_data;       /* pointer naar te displayen data */
	ASM_SAMPLE *temp;                  /* tijdelijke copy voor scalen */

	vscale=Window_VScale(vp);
	hscale=Window_HScale(vp);
	hoogte=Window_Height(vp) - TOP_YOFFS - BOTTEM_YOFFS;


	temp = (ASM_SAMPLE *) malloc((unsigned int)(DataLength(vp)*DataRecords(vp))*sizeof(ASM_SAMPLE));
	if (temp == NULL)
	{
		asm_wprintf("Error in gen_phase_plot: malloc failed (temp)");
		return;
	}

	real_data = DataReal(vp);     /* display real data */

	/* Bereken het adres van het channel dat je wil zien.
   In het fase domein bevatten channels altijd maar 1 record */

	real_data = real_data + Window_Channel(vp)*DataRecords(vp)*DataLength(vp);

	/*  asm_wprintf("Signal: %s, Channel: %d\n",DataSignalName(vp),Window_Channel(vp));
  asm_wprintf("VSCALE : %g \n",Window_VScale(vp)); */


	for (i=0;i < DataLength(vp);i++)
		temp[i]=(real_data[i])/Window_VScale(vp);


	nul_offs = TOP_YOFFS+(Window_Max(vp)/Window_VScale(vp));


	if (!bon)
	{
		for(i=0;i < (DataLength(vp)-1);i++)
		{
			line(asm_draw_bitmap , (int)(hscale*i+LEFT_XOFFS) , (int)-(temp[i]-nul_offs), (int)(hscale*(i+1) +
			    LEFT_XOFFS), (int)-(temp[i+1]-nul_offs), ASM_GREEN_COLOR);
		}
	}
	else
	{
		for(i=0;i < (DataLength(vp));i++)
		{
			line(asm_draw_bitmap , (int)(hscale*i+LEFT_XOFFS) , (int)(nul_offs), (int)(hscale*i+LEFT_XOFFS),
			    (int)-(temp[i]-nul_offs), ASM_GREEN_COLOR);
		}
	}


	free(temp);
}

/************************************************************************************/

int make_bitmap(VENSTER *vp)
{

	/* destroy previous asm_draw_bitmap */
	if (asm_draw_bitmap != NULL)
		destroy_bitmap(asm_draw_bitmap);

	Window_Width(vp)=Window_HScale(vp)*DataLength(vp) + LEFT_XOFFS + RIGHT_XOFFS;
	if (Window_Mode(vp) == BODE_M) {
		Window_Height(vp)= BODE_SIGNAL_HOOGTE + (int)(BODE_SIGNAL_HOOGTE/PH_MAG) + 2*TOP_YOFFS + BOTTEM_YOFFS;
	} else {
		Window_Height(vp)= (SIGNAL_HOOGTE + TOP_YOFFS + BOTTEM_YOFFS);
	}


	if( (asm_draw_bitmap = create_bitmap(Window_Width(vp),Window_Height(vp))) == NULL) {
		asm_wprintf("Error: cannot create asm_draw_bitmap");
		asm_wprintf("Try to make it smaller with the 'xscale' command");
		return -1;
	} else
           clear_to_color(asm_draw_bitmap, gui_fg_color);

	/* clear the screen */
	/* A bode diagram is higher so we take
	  this height */
	rectfill(screen,ASM_DRAWING_LEFT ,text_height(font),SCREEN_W,text_height(font) + BODE_SIGNAL_HOOGTE + (int)(BODE_SIGNAL_HOOGTE/PH_MAG) +
	    2*TOP_YOFFS + BOTTEM_YOFFS,gui_bg_color);


	switch (Window_Mode(vp))
	{
	case REAL_M:

	case IMAG_M:
		if (DataDomain(vp) == TIME)
			gen_signal_background(vp);
		if (DataDomain(vp) == FREQ)
			gen_freq_background(vp);

		gen_signal_plot(vp);
		break;

	case BODE_M:
		gen_bode_background(vp);
		gen_bode_plot(vp,0);
		break;
	case MAGN_M:
		gen_magnitude_background(vp);
		gen_magnitude_plot(vp,0);
		break;
	case PHAS_M:
		gen_phase_background(vp);
		gen_phase_plot(vp,0);
		break;
	case HIST_M:
		gen_hist_background(vp);
		gen_hist_plot(vp);
		break;
	default:
		break;
	}
        return 0;
}

int make_plot(VENSTER *vp)
{
        int i;

        i = make_bitmap(vp);
        if (i == -1)
            return -1;

	if (Window_Width(vp) > SCREEN_W)
		i = SCREEN_W;
	else i = Window_Width(vp);

	blit(asm_draw_bitmap,screen ,0 ,0 ,ASM_DRAWING_LEFT ,text_height(font) ,i ,Window_Height(vp));


	/* text_mode(gui_bg_color); */
	return 0;
}

void plot_x_offset(VENSTER *vp, int x_offset) {

	if (asm_draw_bitmap == NULL)
            return;

        rectfill(screen,ASM_DRAWING_LEFT ,text_height(font),SCREEN_W,text_height(font)+(asm_draw_bitmap->h),
            gui_bg_color);
        blit(asm_draw_bitmap,screen ,x_offset ,0 ,ASM_DRAWING_LEFT ,text_height(font) ,SCREEN_W
            ,asm_draw_bitmap->h);
}
