/*************************************************************************************
 *                                                                                   *
 * File: SOURCES.C                                                                   *
 *                                                                                   *
 * Inhoud: Deze file bevat veschillende functies die signalen kunnen genereren.      *
 *         Deze functies worden door de command line interpreter aangeroepen.        *
 *
 * FdeltaCi(char *s), FstepCi(char *p), FsquarewaveCi(char *p), FrampCi(char *s),
   FtriangleCi(char *p), FsineCi(char *p), FsinecCi(char *p),
   FcosineCi(char *p), FexpCi(char *p), FnoiseCi(char *p),
   WhanningCi(char *p), WhammingCi(char *p), WgaussCi(char *p),
   WblackmanCi(char *p), WkaiserCi(char *p), WtriangleCi(char *p).

  Geschreven door: Edwin Zoer & Erwin Waterlander, HTS Enschede.

*/


#include <string.h>
#include <math.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>
#include <math.h>
#include "allegro.h"  /* only for TRUE definition */
#include "asm.h"
#include "parse.h"
#include "commnd.h"
#include "image.h"
#include "errors.h"
#include "imagwind.h"
#include "plotutil.h"
#include "windows.h"

#define DEF_N 9              /* 2^9 = 512 = default aantal samples */
#define DEF_FREQ 100         /* Default frequentie */
#define DEF_AMPL 100         /* Default amplitude */

#define PI M_PI

#define ASMSIGNAL 19

extern VENSTER* vp[MAXSIG];
extern unsigned short MAX_LENGTH;
extern unsigned short SAMPLE_RATE;
extern BOOL don;
extern int current_signal;  /* The actual signal drawn to screen */

/********************************************************************************
 * int FnoiseCi(char *p)                                                         *
 * input:   signaalnaam;                                                        *
 *          datatype (reeel,imag,complex);                                      *
 *          seed (welke willekeurige reeks);                                    *
 *          aantal elementen;                                                   *
 * output:  een of twee arrays gevuld met willekeurige getallen;                *
 *          drawobject voor signaal;                                            *
 * returns: ERR_WINDOWS, te veel displaywindows                                 *
 *          0, geen errors                                                      *
 ********************************************************************************/

int FnoiseCi(char *p)
{

	time_t timer;
	char *ctijd;
	int i,im,n,seed;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];
	double ampl;

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);
	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	seed   = (short) intarg(&p,NULL,"Seed <0 .. 512>",0,512,1);
	n = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	number = pow(2,n);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);




	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"noise",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;


	srand((unsigned int) seed);
	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	switch (dtype)
	{
	case REAL:                        /* Reele data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)((ampl/(RAND_MAX/2.0))*(rand() - (RAND_MAX/2.0)));
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:                       /* Imaginaire data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = (ASM_SAMPLE)((ampl/(RAND_MAX/2.0))*(rand() - (RAND_MAX/2.0)));
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:                       /* Complexe data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)((ampl/(RAND_MAX/2.0))*(rand() - (RAND_MAX/2.0)));
			imag_data[i] = (ASM_SAMPLE)((ampl/(RAND_MAX/2.0))*(rand() - (RAND_MAX/2.0)));
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}

	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;
	return 0;

}

/********************************************************************
 * FsineCi genereerdt een sinus. De data is van het type ASM_SAMPLE
 */

int FsineCi (char *s)
{

	time_t timer;
	char *ctijd;
	int i,im,n,sampler;
	double phi0,freq,ampl,offs;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&s,NULL,"Signal","a",buffer);

	offs  = doublearg(&s,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = doublearg(&s,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	freq  = doublearg(&s,NULL,"Frequency",1,INT_MAX,DEF_FREQ);
	phi0  = doublearg(&s,NULL,"Phase (rad)",0,2*PI,0);
	dtype  = (short) intarg(&s,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	n = intarg(&s,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&s,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	number = pow(2,n);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */

	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);

	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"sine",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;


	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	sampler = sample*10;


	switch (dtype)
	{
	case REAL:              /* Reele data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)(offs + ampl*sin(2*PI*freq*i/sampler + phi0));
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:              /* Imaginaire data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = (ASM_SAMPLE)(offs + ampl*sin(2*PI*freq*i/sampler + phi0));
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:              /* Complexe data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)(offs + ampl*sin(2*PI*freq*i/sampler + phi0));
			imag_data[i] = (ASM_SAMPLE)(offs + ampl*sin(2*PI*freq*i/sampler + phi0));
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;
	return 0;

}
/********************************************************************
 * FsinecCi genereerdt een (sinus(t)) / t. De data is van het type SAMPLE
 */

int FsinecCi(char *p)
{

	time_t timer;
	char *ctijd;
	int i,im,n,sampler;
	double freq,ampl,offs,t,arg;
	int number,sample;
	unsigned int alloc_size;
	short dtype;

	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	offs  = doublearg(&p,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	freq  = doublearg(&p,NULL,"Frequency",1,INT_MAX,DEF_FREQ);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	n = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,n);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);

	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));


	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"sinc",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	sampler = sample*10;


	switch (dtype)
	{
	case REAL:              /* Reele data */
		for (i=0;i < number;i++)
		{
			t = (double)(i-number/2)/(double)sampler;
			arg = 2*PI*freq*t;
			if (t!=0.0)
				real_data[i] = (ASM_SAMPLE)(offs + ampl*sin(arg)/arg);
			else real_data[i] = (ASM_SAMPLE)(offs + ampl*cos(arg));
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:              /* Imaginaire data */
		for (i=0;i < number;i++)
		{
			t = (double)(i-number/2)/(double)sampler;
			arg = 2*PI*freq*t;
			real_data[i] = (ASM_SAMPLE)0;
			if (t!=0.0)
				imag_data[i] = (ASM_SAMPLE)(offs + ampl*sin(arg)/arg);
			else imag_data[i] = (ASM_SAMPLE)(offs + ampl*cos(arg));
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:              /* Complexe data */
		for (i=0;i < number;i++)
		{
			t = (double)(i-number/2)/(double)sampler;
			arg = 2*PI*freq*t;
			if (t!=0.0)
			{
				real_data[i] = (ASM_SAMPLE)(offs + ampl*(sin(arg))/arg);
				imag_data[i] = (ASM_SAMPLE)(offs + ampl*(sin(arg))/arg);
			}
			else
			{
				real_data[i] = (ASM_SAMPLE)(offs + ampl*cos(arg));
				imag_data[i] = (ASM_SAMPLE)(offs + ampl*cos(arg));
			}
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}

	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;

}
/************************************************************************
 * FcosineCi genereerdt een cosinus. De data is van het type ASM_SAMPLE
 */

int FcosineCi(char *p)
{

	time_t timer;
	char *ctijd;
	int i,im,n,sampler;
	double phi0,freq,ampl,offs;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	offs  = doublearg(&p,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	freq  = doublearg(&p,NULL,"Frequency",1,INT_MAX,DEF_FREQ);
	phi0  = doublearg(&p,NULL,"Phase (rad)",0,2*PI,0);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	n = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,n);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);

	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"cosine",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	sampler = sample*10;


	switch (dtype)
	{
	case REAL:              /* Reele data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)(offs + ampl*cos(2*PI*freq*i/sampler + phi0));
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:              /* Imaginaire data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = (ASM_SAMPLE)(offs + ampl*cos(2*PI*freq*i/sampler + phi0));
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:              /* Complexe data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)(offs + ampl*cos(2*PI*freq*i/sampler + phi0));
			imag_data[i] = (ASM_SAMPLE)(offs + ampl*cos(2*PI*freq*i/sampler + phi0));
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;       /* No Error */

}
/********************************************************************
 * FexpCi genereerdt een A*(1-e^(-t/T)). De data is van het type ASM_SAMPLE
 */

int FexpCi(char *p)
{

	time_t timer;
	char *ctijd;
	int i,im,n,sampler;
	double ampl,t,T;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	T     = doublearg(&p,NULL,"t 63.2% (millisec)",0.000001,1000000.0,10.0);
	dtype = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	n     = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,n);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));


	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])=sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"exp",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;


	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	sampler = sample*10;
	T = T/1000.0;              /* T in sec */

	switch (dtype)
	{
	case REAL:              /* Reele data */
		for (i=0;i < number;i++)
		{
			t = (double)i/(double)sampler;
			real_data[i] = ampl*(exp(-t/T));
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:              /* Imaginaire data */
		for (i=0;i < number;i++)
		{
			t = (double)i/(double)sampler;
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = ampl*(exp(-t/T));
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:              /* Complexe data */
		for (i=0;i < number;i++)
		{
			t = (double)i/(double)sampler;
			real_data[i] = ampl*(exp(-t/T));
			imag_data[i] = ampl*(exp(-t/T));
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}




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

int FsquarewaveCi(char *p)

{

	time_t timer;
	char *ctijd;
	int i,n,num,im;
	double offs,ampl,freq,duty_cycle,dc,sampler,T,t,B;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	offs  = doublearg(&p,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	freq  = doublearg(&p,NULL,"Frequency",1,INT_MAX,DEF_FREQ);
	duty_cycle = doublearg(&p,NULL,"Duty-cycle <0% .. 100%>",0,100,50);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	num     = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);

	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"block",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	sampler = sample*10.0;
	dc = duty_cycle/100.0;
	T=1/freq;                  /* Periode tijd */

	switch (dtype)
	{
	case REAL:           /* Reele data */
		n = 0;
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if ((t-T*n) <  (dc*T)){ 
				B = 1;  
			}
			else
			{ 
				B =-1;  
			}
			real_data[i] = (ASM_SAMPLE)(offs + ampl*B);
			imag_data[i] = (ASM_SAMPLE)0;
			if (t >= ((n+1)*T)) n++;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:
		n = 0;
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if ((t-T*n) <  (dc*T)){ 
				B = 1;  
			}
			else
			{ 
				B =-1;  
			}
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = (ASM_SAMPLE)(offs + ampl*B);
			if (t >= ((n+1)*T)) n++;
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:
		n = 0;
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if ((t-T*n) <  (dc*T)){ 
				B = 1;  
			}
			else
			{ 
				B =-1;  
			}
			real_data[i] = (ASM_SAMPLE)(offs + ampl*B);
			imag_data[i] = (ASM_SAMPLE)(offs + ampl*B);
			if (t >= ((n+1)*T)) n++;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;             /* No Error */
}



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

int FtriangleCi(char *p)

{

	time_t timer;
	char *ctijd;
	int i,n,im,num,sampler;
	double offs,ampl,freq,T,t,t0;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];
	double rc,phi0;

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	offs  = doublearg(&p,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = intarg(&p,NULL,"Amplitude",1,INT_MAX,DEF_AMPL);
	freq  = intarg(&p,NULL,"Frequency",1,INT_MAX,DEF_FREQ);
	phi0  = doublearg(&p,NULL,"Phase (rad)",0,2*PI,0);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"triangle",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	sampler = sample*10;
	T=(double)(1)/(double)(freq);        /* periode tijd */
	rc = 4*ampl*freq;    /* richtings coefficient */
	t0 = (phi0*T)/(double)(2*PI);

	switch (dtype)
	{
	case REAL:           /* Reele data */
		n = 0;
		for (i=0;i < number;i++)
		{
			t=(double)(i)/(double)(sampler);
			if ((t-T*n+t0) <  (T/2)){
				real_data[i] = (ASM_SAMPLE)(rc*(t-n*T+t0) - ampl + offs);
			}
			if ((t-T*n+t0) >= (T/2)){
				real_data[i] = (ASM_SAMPLE)(-rc*(t-n*T+t0) + 3*ampl + offs);
			}
			imag_data[i] = (ASM_SAMPLE)0;
			if ((t+t0) >= ((n+1)*T)) n++;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:
		n = 0;
		for (i=0;i < number;i++)
		{
			t=(double)(i)/(double)(sampler);
			real_data[i] = (ASM_SAMPLE)0;
			if ((t-T*n+t0) <  (T/2 )){
				imag_data[i] = (ASM_SAMPLE)(rc*(t-n*T+t0) - ampl + offs);
			}
			if ((t-T*n+t0) >= (T/2 )){
				imag_data[i] = (ASM_SAMPLE)(-rc*(t-n*T+t0) + 3*ampl + offs);
			}
			if ((t+t0) >= ((n+1)*T)) n++;
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:
		n=0;
		for (i=0;i < number;i++)
		{
			t=(double)(i)/(double)(sampler);
			if ((t-T*n+t0) <  (T/2)){
				real_data[i] = (ASM_SAMPLE)(rc*(t-n*T+t0) - ampl + offs);
			}
			if ((t-T*n+t0) >= (T/2)){
				real_data[i] = (ASM_SAMPLE)(-rc*(t-n*T+t0) + 3*ampl + offs);
			}
			if ((t-T*n+t0) <  (T/2)){
				imag_data[i] = (ASM_SAMPLE)(rc*(t-n*T+t0) - ampl + offs);
			}
			if ((t-T*n+t0) >= (T/2)){
				imag_data[i] = (ASM_SAMPLE)(-rc*(t-n*T+t0) + 3*ampl + offs);
			}
			if ((t+t0) >= ((n+1)*T)) n++;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}

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

int FconstCi(char *p)

{
	time_t timer;
	char *ctijd;
	int i,im,num,sample;
	double ampl;
	int number;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"constant",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	switch (dtype)
	{
	case REAL:           /* Reele data */
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)(ampl);
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = (ASM_SAMPLE)(ampl);
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:
		for (i=0;i < number;i++)
		{
			real_data[i] = (ASM_SAMPLE)(ampl);
			imag_data[i] = (ASM_SAMPLE)(ampl);
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}



	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;   /* No Error */
}


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

int FstepCi(char *p)

{

	time_t timer;
	char *ctijd;
	int i,im,num;
	double offs,ampl,sampler,t,t0;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;


	signalname = strarg(&p,NULL,"Signal","a",buffer);

	offs  = doublearg(&p,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,DEF_AMPL);
	t0    = doublearg(&p,NULL,"t-delay (ms)",0,INT_MAX,0);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);


	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"step",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;


	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	sampler = sample*10;  /* sample rate in Hz */
	t0 = t0/1000.0;          /* t0 in seconden */

	switch (dtype)
	{
	case REAL:           /* Reele data */
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if (t <  t0) real_data[i] = (ASM_SAMPLE)(offs);
			if (t >= t0) real_data[i] = (ASM_SAMPLE)(ampl+offs);
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			real_data[i] = (ASM_SAMPLE)0;
			if (t <  t0) imag_data[i] = (ASM_SAMPLE)(offs);
			if (t >= t0) imag_data[i] = (ASM_SAMPLE)(ampl+offs);
		}

		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if (t <  t0) real_data[i] = (ASM_SAMPLE)(offs);
			if (t >= t0) real_data[i] = (ASM_SAMPLE)(ampl+offs);
			if (t <  t0) imag_data[i] = (ASM_SAMPLE)(offs);
			if (t >= t0) imag_data[i] = (ASM_SAMPLE)(ampl+offs);
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;

	return 0;       /* No Error */
}

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


int FdeltaCi(char *p)

{

	time_t timer;
	char *ctijd;
	int i,im,num;
	double ampl,sampler,t,t0;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];
	BOOL flag;

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;



	signalname = strarg(&p,NULL,"Signal","a",buffer);

	ampl  = doublearg(&p,NULL,"Amplitude",0,INT_MAX,1);
	t0    = doublearg(&p,NULL,"Delta-t (millisec)",0,INT_MAX,0);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"delta",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);



	sampler = sample*10;


	flag = TRUE;
	t0 = t0/1000.0;  /* t0 in sec */

	switch (dtype)
	{
	case REAL:           /* Reele data */
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if (t < t0) real_data[i] = (ASM_SAMPLE)(0);
			else if ((t >= t0) && flag)
			{
				real_data[i] =(ASM_SAMPLE)(ampl);
				flag = FALSE;
			}
			else real_data[i] =(ASM_SAMPLE)(0);
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			real_data[i] = (ASM_SAMPLE)0;
			if (t < t0) imag_data[i] = (ASM_SAMPLE)(0);
			else if ((t >= t0) && flag)
			{
				imag_data[i] =(ASM_SAMPLE)(ampl);
				flag = FALSE;
			}
			else imag_data[i] =(ASM_SAMPLE)(0);

		}

		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:
		for (i=0;i < number;i++)
		{
			t=i/sampler;
			if (t < t0)
			{
				real_data[i] = (ASM_SAMPLE)(0);
				imag_data[i] = (ASM_SAMPLE)(0);
			}
			else if ((t >= t0) && flag)
			{
				real_data[i] =(ASM_SAMPLE)(ampl);
				imag_data[i] =(ASM_SAMPLE)(ampl);
				flag = FALSE;
			}
			else {
				real_data[i] =(ASM_SAMPLE)(0);
				imag_data[i] = (ASM_SAMPLE)0;
			}
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;       /* No Error */
}

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

int FrampCi(char *p)

{

	time_t timer;
	char *ctijd;
	int i,im,num,sampler;
	double offs,ampl,t;
	int number,sample;
	unsigned int alloc_size;
	short dtype;
	char *signalname;
	char buffer[128];
	double rc;

	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer);

	offs  = doublearg(&p,NULL,"Offset",INT_MIN,INT_MAX,0);
	ampl  = intarg(&p,NULL,"Amplitude",1,INT_MAX,DEF_AMPL);
	dtype  = (short) intarg(&p,NULL,"Type <0=Real,1=Imag,2=Complex>",REAL,COMP,REAL);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= dtype;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"ramp",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	sampler = sample*10;                        /* sample rate */

	rc = ampl*(double)sampler/(double)(number-1);    /* richtings coefficient */

	switch (dtype)
	{
	case REAL:           /* Reele data */
		for (i=0;i < number;i++)
		{
			t=(double)(i)/(double)(sampler);
			real_data[i] = (ASM_SAMPLE)(rc*t + offs);
			imag_data[i] = (ASM_SAMPLE)0;
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	case IMAG:
		for (i=0;i < number;i++)
		{
			t=(double)(i)/(double)(sampler);
			real_data[i] = (ASM_SAMPLE)0;
			imag_data[i] = (ASM_SAMPLE)(rc*t + offs);
		}
		Window_Mode(vp[im])=IMAG_M;
		break;
	case COMP:
		for (i=0;i < number;i++)
		{
			t=(double)(i)/(double)(sampler);
			real_data[i] = (ASM_SAMPLE)(rc*t + offs);
			imag_data[i] = (ASM_SAMPLE)(rc*t + offs);
		}
		Window_Mode(vp[im])=REAL_M;
		break;
	default: 
		Window_Mode(vp[im])=REAL_M;
		break;
	}


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}

/********************************************************************************
*   WblockCi genereerd een block
*********************************************************************************/

int WblockCi(char *p)
{

	time_t timer;
	char *ctijd;
	int i,im,windowing,num;
	int number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 0;   /* Block window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Block window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);



	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}

/********************************************************************************
*   WhanningCi genereerd een hanning-curve
*********************************************************************************/

int WhanningCi(char *p)
{

	time_t timer;
	char *ctijd;
	int i,im,windowing,num;
	int number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 1;   /* Hanning window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Hanning window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);



	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}


/********************************************************************************
*   WhammingCi genereerd een hamming-curve
*********************************************************************************/

int WhammingCi(char *p)
{
	time_t timer;
	char *ctijd;
	int i,im,windowing,num;
	int number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 2;   /* Hamming window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Hamming window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}

/********************************************************************************
*   WgaussCi genereerd een Gauss-curve
*********************************************************************************/

int WgaussCi(char *p)
{
	time_t timer;
	char *ctijd;
	int i,im,windowing,num;
	int number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 3;   /* Gauss window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Gauss window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;
}

/********************************************************************************
*   WblackmanCi genereerd een Blackman-curve
*********************************************************************************/

int WblackmanCi(char *p)
{
	time_t timer;
	char *ctijd;
	int i,im,windowing,num,number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 4;   /* Blackman window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);

	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Blackman window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;    /* No Error */
}


/********************************************************************************
*   WkaiserCi genereerd een Kaiser-curve
*********************************************************************************/

int WkaiserCi(char *p)
{
	time_t timer;
	char *ctijd;
	int i,im,windowing,num,number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 5;   /* Kaiser window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	signalname[sizeof(DataSignalName(vp[im]))-1] = '\0';
	strncpy(DataSignalName(vp[im]),signalname,sizeof(DataSignalName(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Kaiser window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;    /* No Error */
}

/********************************************************************************
*   WtriangleCi genereerd een driehoek-curve
*********************************************************************************/

int WtriangleCi(char *p)
{
	time_t timer;
	char *ctijd;
	int i,im,windowing,num,number,sample;
	unsigned int alloc_size;
	char *signalname;
	char buffer1[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

	signalname = strarg(&p,NULL,"Signal","a",buffer1);
	num   = intarg(&p,NULL,"Number of elements (2^n)",MIN_N,MAX_N,DEF_N);
	sample = (unsigned short) intarg(&p,NULL,"Samplerate in 10Hz",1,USHRT_MAX,SAMPLE_RATE);

	windowing = 6;   /* triangle window  */

	number = pow(2,num);
	alloc_size = number; /* Geheugenruimte voor data (1 channel en 1 record) */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);


	time(&timer);
	ctijd = asctime(localtime(&timer));
	ctijd[sizeof(DataDate(vp[im]))-1] = '\0';
	strncpy(DataDate(vp[im]),ctijd,sizeof(DataDate(vp[im])));

	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= number;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)BPS_SAMPLE;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= sample;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= COMP;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"Blackman window",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = 1;
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;
	Window_Mode(vp[im]) = REAL_M;

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	for (i=0;i < number;i++)
	{
		real_data[i] = (ASM_SAMPLE)1;
		imag_data[i] = (ASM_SAMPLE)1;
	}

	Window_functions(real_data,imag_data,real_data,imag_data,number,windowing);


	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;


	return 0;      /* No Error */
}
