/*************************************************************************
 *                                                                       *
 * File: CALCULATE.C                                                     *
 *                                                                       *
 * Inhoud: Deze file bevat alle rekenkundige functies die door de CLI    *
 *         aangeroepen kunnen worden.                                    *
 *                                                                       *
   int ClearCi(char *p),int AssignCi(char *p),int CmultiplyCi(char *p),
   int CdivideCi(char *p),int CabsCi(char *p),int CaddCi(char *p),
   int InvCi(char *p),int ConjugateCi(char *p), int AddCi(char *p),
   int SubCi(char *p),
   int MultiplyCi(char *p),int DivideCi(char *p),int AbsoluteCi(char *p),
   int LnCi(char *p),int LogCi(char *p),int ExpCi(char *p),
   int TenCi(char *p),
   int SineCi(char *p),int CosineCi(char *p),int MinimumCi(char *p),
   int MaximumCi(char *p), int ZeropadCi(char *p), int ShiftCi(char *p)
   int RotateCi(char *p), int ClipCi(char *p)

   Geschreven door : Edwin Zoer & Erwin Waterlander, HTS Enschede
   Datum: juni 1993

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


#include <string.h>
#include <math.h>
#include <stdio.h>
#include <limits.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"


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

#define ASMSIGNAL 19

extern BOOL don;

//extern void display_signal_plot(VENSTER *vp);
//extern void gen_signal_background(VENSTER *vp);
//extern void gen_freq_background(VENSTER *vp);
//extern char *GenIm;   /* c.aim */
//extern char *Image;   /* c.aim */
extern VENSTER* vp[MAXSIG];
extern unsigned short MAX_LENGTH;
extern unsigned short SAMPLE_RATE;
extern int current_signal;  /* The actual signal drawn to screen */
//extern int Find_Signal_Venster(char *signalname);    /* command.c */
//extern int Create_Venster(char *signalname,unsigned int Alloc_Size);         /* command.c */
//extern int Realloc_Venster(VENSTER *vp, unsigned int alloc_size);

/*****************************************************************************
 * int ClearCi(char *p)                                                      *
 * input:   signaalnaam                                                      *
 * returns: ERR_SIGNAL, het betreffende signaal bestaat niet;                *
 *          0, geen error.                                                   *
 *****************************************************************************/

int ClearCi(char *p)
{
	int im;                 /* Vensternummer */
	int i;
	char *signalname;
	char buffer[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

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


	im = Find_Signal_Venster(signalname);
	if (im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

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


	for (i=0;i < (DataChannels(vp[im])*DataRecords(vp[im])*DataLength(vp[im])) ;i++)
	{

		real_data[i] = 0;
		imag_data[i] = 0;
	}


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

	return 0;
}



/*****************************************************************************
 * int AssignCi(char *p)                                                      *
 * input:   signaalnaam                                                      *
 * returns: ERR_SIGNAL, het betreffende signaal bestaat niet;                *
 *          0, geen error.                                                   *
 *****************************************************************************/

int AssignCi(char *p)
{
	int im;                 /* Vensternummer */
	int i;
	double real_constant,imag_constant;
	char *signalname;
	char buffer[128];
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

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

	im = Find_Signal_Venster(signalname);
	if (im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	switch (DataType(vp[im]))
	{
	case REAL:
		real_constant = doublearg(&p,NULL,"Real part",INT_MIN,INT_MAX,1);
		imag_constant = 0.0;
		break;
	case IMAG:
		imag_constant   = doublearg(&p,NULL,"Imag part",INT_MIN,INT_MAX,1);
		real_constant = 0.0;
		break;
	case COMP:
		real_constant = doublearg(&p,NULL,"Real part",INT_MIN,INT_MAX,1);
		imag_constant = doublearg(&p,NULL,"Imag part",INT_MIN,INT_MAX,1);
		break;
	default:
		imag_constant = 0.0;
		real_constant = 0.0;
		break;
	}
	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);


	for (i=0;i < (DataChannels(vp[im])*DataRecords(vp[im])*DataLength(vp[im])) ;i++)
	{
		real_data[i] = real_constant;
		imag_data[i] = imag_constant;
	}

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

	return 0;
}

/********************************************************************************
*   CmultiplyCi vermenigvuldigd een array met een constante
*********************************************************************************/

int CmultiplyCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	double mul_factor;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	mul_factor     = doublearg(&p,NULL,"Constant",INT_MIN,INT_MAX,1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;                /* Signaal bestaat niet */

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,32);

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im])) ;i++)
	{

		real_data_out[i] = mul_factor*real_data_in[i];
		imag_data_out[i] = mul_factor*imag_data_in[i];
	}


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

	return 0;
}



/********************************************************************************
*   CdivideCi deelt een array door een constante
*********************************************************************************/

int CdivideCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	double div_factor;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	div_factor     = doublearg(&p,NULL,"Constant",1,INT_MAX,1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = real_data_in[i]/div_factor;
		imag_data_out[i] = imag_data_in[i]/div_factor;
	}


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


/********************************************************************************
*   CabsCi bepaald de absolute waarde
*********************************************************************************/

int CabsCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */


	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	DataType(vp[out_im])=REAL;
	if (Window_Mode(vp[in_im]) == IMAG_M)
		Window_Mode(vp[out_im])=REAL_M;
	else
		Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);

	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = sqrt( pow(real_data_in[i],2) + pow(imag_data_in[i],2));
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}

/********************************************************************************
*   CaddCi telt een constante op bij een array
*********************************************************************************/

int CaddCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	double add_const;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	add_const      = doublearg(&p,NULL,"Constant",INT_MIN,INT_MAX,0);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = real_data_in[i]+add_const;
		imag_data_out[i] = imag_data_in[i];
	}


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


/********************************************************************************
*   InvCi inverteerd een array
*********************************************************************************/

int InvCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = -real_data_in[i];
		imag_data_out[i] = -imag_data_in[i];
	}


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

	return 0;
}

/********************************************************************************
*   ConjugateCi: Bepaal de complex-toegevoegde.
*********************************************************************************/

int ConjugateCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	if( DataDomain(vp[out_im]) != REAL )
	{
		for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
		{
			real_data_out[i] =  real_data_in[i];
			imag_data_out[i] = -imag_data_in[i];
		}
	}


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

	return 0;
}


/********************************************************************************
*   AddCi telt twee array's bij elkaar op
*********************************************************************************/

int AddCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);

	out_signalname  = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;            /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;            /* Signaal bestaat niet */

	if  (DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]))
		return ERR_DOMAIN;                          /*verkeerde domein  */

	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;



	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);          /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);

	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */

	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));


	if (DataType(vp[in_im1]) != DataType(vp[in_im2])) DataType(vp[out_im])=COMP;

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{

		real_data_out[i] = a[i]+c[i];
		imag_data_out[i] = b[i]+d[i];
	}


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

	return 0;
}

/********************************************************************************
*   SubCi trekt twee array's van elkaar af
*********************************************************************************/

int SubCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if  (DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]))
		return ERR_DOMAIN;                             /*verkeerde domein  */

	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;


	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	if (DataType(vp[in_im1]) != DataType(vp[in_im2])) DataType(vp[out_im])=COMP;

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{

		real_data_out[i] = a[i]-c[i];
		imag_data_out[i] = b[i]-d[i];
	}


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

	return 0;
}


/********************************************************************************
*   MultiplyCi vermenigvuldigd twee array's met elkaar
*********************************************************************************/

int MultiplyCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	double constant;
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;
	ASM_SAMPLE temp_a,temp_c;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);
	constant        = doublearg(&p,NULL,"Constant",1,INT_MAX,1);
	out_signalname  = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]))
		return ERR_DOMAIN;    /*verkeerde domein  */

	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;

	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	if (DataType(vp[in_im1]) != DataType(vp[in_im2])) DataType(vp[out_im])=COMP;

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		temp_a = a[i];
		temp_c = c[i];
		real_data_out[i] = (a[i]*c[i] - b[i]*d[i])/constant;
		imag_data_out[i] = (temp_a*d[i] + b[i]*temp_c)/constant;
	}


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

	return 0;
}


/********************************************************************************
*   DivideCi deelt twee complexe frequentie-array's met elkaar
*********************************************************************************/

int DivideCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);
	out_signalname  = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if ( DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]) )
		return ERR_DOMAIN;    /*verkeerde domein  */

	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;

	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	DataType(vp[out_im])=REAL;
	if (Window_Mode(vp[in_im1]) == IMAG_M)
		Window_Mode(vp[out_im])=REAL_M;
	else
		Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);

	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = sqrt(pow(a[i],2)+pow(b[i],2))/ (1+sqrt(pow(c[i],2)+pow(d[i],2)));
		imag_data_out[i] = 0;
	}


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

	return 0;
}


/********************************************************************************
*   AbsCi geeft het absolute verschil van twee array's
*********************************************************************************/

int AbsoluteCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if  (DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]))
		return ERR_DOMAIN;    /*verkeerde domein  */

	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;

	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));


	DataType(vp[out_im])=REAL;
	if (Window_Mode(vp[in_im1]) == IMAG_M)
		Window_Mode(vp[out_im])=REAL_M;
	else
		Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = sqrt(pow(a[i]-c[i],2) + pow(b[i]-d[i],2));
		imag_data_out[i] = 0;
	}


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

	return 0;
}


/********************************************************************************
*   LnCi bepaalt voor alle waarden van het arry de natuurlijke logaritme
*********************************************************************************/

int LnCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataType(vp[in_im]) != REAL) return ERR_IMPL;   /* nog niet geimplementeerd voor COMPL*/

	real_data_in  = DataReal(vp[in_im]);
    for (i=0;i < (DataChannels(vp[in_im])*DataRecords(vp[in_im])*DataLength(vp[in_im]));i++)
      {
       if( (real_data_in[i]) <= 0.0 )
		{
		  asm_wprintf("Signal %s has negative or zero values.", DataSignalName(vp[in_im]));
		  return 0;
		}
      }

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = log(real_data_in[i]);
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}


/********************************************************************************
*   LogCi bepaalt voor alle waarden van het arry de 10-log
*********************************************************************************/

int LogCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataType(vp[in_im]) != REAL) return ERR_IMPL;   /* nog niet geimplementeerd voor COMPL*/

	  real_data_in  = DataReal(vp[in_im]);

    for (i=0;i < (DataChannels(vp[in_im])*DataRecords(vp[in_im])*DataLength(vp[in_im]));i++)
      {
       if( (real_data_in[i]) <= 0.0 )
		{
		    asm_wprintf("Signal %s has negative or zero values.", DataSignalName(vp[in_im]));
		    return 0;
		}
      }


	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = log10(real_data_in[i]);
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}

/********************************************************************************
*   ExpCi bepaalt voor alle waarden van het arry de e-macht
*********************************************************************************/

int EpowCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataType(vp[in_im]) != REAL) return ERR_IMPL;   /* nog niet geimplementeerd voor COMPL*/

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = exp(real_data_in[i]);
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}

/********************************************************************************
*   TenCi bepaalt voor alle waarden van het arry de 10-macht
*********************************************************************************/

int TenpowCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataType(vp[in_im]) != REAL) return ERR_IMPL;   /* nog niet geimplementeerd voor COMPL*/

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = pow(10,real_data_in[i]);
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}

/********************************************************************************
*   SineCi bepaalt voor alle waarden van het signaal de sinus
*********************************************************************************/

int SineCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataType(vp[in_im]) != REAL) return ERR_IMPL;   /* nog niet geimplementeerd voor COMPL*/

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = sin(real_data_in[i]);
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}

/********************************************************************************
*   CosineCi bepaalt voor alle waarden van het signaal de cosinus
*********************************************************************************/

int CosineCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if (DataType(vp[in_im]) != REAL) return ERR_IMPL;   /* nog niet geimplementeerd voor COMPL*/

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		real_data_out[i] = cos(real_data_in[i]);
		imag_data_out[i] = 0.0;
	}


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

	return 0;
}


/********************************************************************************
*   MinimumCi minimum van twee array's
*********************************************************************************/

int MinimumCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);

	out_signalname  = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if  (DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]))
		return ERR_DOMAIN;    /*verkeerde domein  */

	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;


	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));


	DataType(vp[out_im])=REAL;
	Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{

		if ( (pow(a[i],2)+pow(b[i],2)) <= (pow(c[i],2)+pow(d[i],2)) )
		{
			real_data_out[i] = a[i];
			imag_data_out[i] = b[i];
		}
		else
		{
			real_data_out[i] = c[i];
			imag_data_out[i] = d[i];
		}
	}

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

	return 0;
}

/********************************************************************************
*   MaximumCi maximum van twee array's
*********************************************************************************/

int MaximumCi(char *p)
{
	int in_im1,in_im2,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	char *in_signalname1;
	char *in_signalname2;
	char *out_signalname;
	char buffer1[128],buffer2[128],buffer3[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *a,*b,*c,*d;

	in_signalname1  = strarg(&p,NULL,"Input Signal1","a",buffer1);
	in_signalname2  = strarg(&p,NULL,"Input Signal2","b",buffer2);

	out_signalname  = strarg(&p,NULL, "Output Signal",in_signalname2,buffer3);

	in_im1 = Find_Signal_Venster(in_signalname1);
	if (in_im1 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	in_im2 = Find_Signal_Venster(in_signalname2);
	if (in_im2 == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */


	if ( (DataLength(vp[in_im1]) != DataLength(vp[in_im2])) || (DataRecords(vp[in_im1]) != DataRecords(vp[in_im2])) ||
	    (DataChannels(vp[in_im1]) != DataChannels(vp[in_im2]))  )
		return ERR_SIZE;

	if  (DataDomain(vp[in_im1]) != DataDomain(vp[in_im2]))
		return ERR_DOMAIN;    /*verkeerde domein  */

	alloc_size = DataAllocSize(vp[in_im1]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	a = DataReal(vp[in_im1]);
	b = DataImag(vp[in_im1]);
	c = DataReal(vp[in_im2]);
	d = DataImag(vp[in_im2]);


	(*vp[out_im]).header = (*vp[in_im1]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;
	strncpy(DataSignalName(vp[out_im]),out_signalname,sizeof(DataSignalName(vp[out_im])));

	DataType(vp[out_im])=REAL;
	Window_Mode(vp[out_im])= Window_Mode(vp[in_im1]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im1]);

	for (i=0;i < (DataChannels(vp[out_im])*DataRecords(vp[out_im])*DataLength(vp[out_im]));i++)
	{
		if ( (pow(a[i],2)+pow(b[i],2)) >= (pow(c[i],2)+pow(d[i],2)) )
		{
			real_data_out[i] = a[i];
			imag_data_out[i] = b[i];
		}
		else
		{
			real_data_out[i] = c[i];
			imag_data_out[i] = d[i];
		}
	}


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

	return 0;
}

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


int ZeropadCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	unsigned int alloc_size;
	unsigned int offset_in,offset_out;
	unsigned short channel,record,temp_length;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;
	ASM_SAMPLE *imag_hulp,*real_hulp;


	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;       /* Signaal bestaat niet */

	if(DataDomain(vp[in_im]) != TIME) return ERR_DOMAIN;
	if(DataLength(vp[in_im]) > (pow(2,(MAX_N-1)))) return ERR_LONG;

	alloc_size = 2*DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */


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

	real_data_out = DataReal(vp[out_im]);    /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	if (in_im == out_im)
	{
		real_hulp=imag_hulp=NULL;
		real_hulp = (ASM_SAMPLE*) malloc((unsigned int)DataAllocSize(vp[in_im])*sizeof(ASM_SAMPLE));
		imag_hulp = (ASM_SAMPLE*) malloc((unsigned int)DataAllocSize(vp[in_im])*sizeof(ASM_SAMPLE));
		if ( (imag_hulp == NULL) || (real_hulp == NULL) )
			asm_wprintf("Error in ZeropadCi: malloc failed\r");


		for (i=0;i<(DataChannels(vp[in_im])*DataRecords(vp[in_im])*DataLength(vp[in_im]));i++)
		{
			real_hulp[i] = real_data_in[i];
			imag_hulp[i] = imag_data_in[i];
		}
		Realloc_Venster(vp[out_im],alloc_size);
		real_data_in  = real_data_out = DataReal(vp[out_im]);
		imag_data_in  = imag_data_out = DataImag(vp[out_im]);
		for (i=0;i<(DataChannels(vp[in_im])*DataRecords(vp[in_im])*DataLength(vp[in_im]));i++)
		{
			real_data_in[i] = real_hulp[i];
			imag_data_in[i] = imag_hulp[i];
		}
		free(real_hulp);
		free(imag_hulp);
	}


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */

	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	temp_length = DataLength(vp[in_im]);
	DataLength(vp[out_im]) = 2*DataLength(vp[in_im]);

	strncpy(DataSignalName(vp[out_im]),out_signalname,32);

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])= 0;
	Window_Channel(vp[out_im])= 0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	for (channel = 0;channel < DataChannels(vp[in_im]); channel++)
	{
		for (record = 0;record < DataRecords(vp[in_im]); record++)
		{
			/* offset betreffende record */
			offset_in = (channel*DataRecords(vp[in_im]) + record)*DataLength(vp[in_im]);
			offset_out = (channel*DataRecords(vp[out_im]) + record)*DataLength(vp[out_im]);
			for (i=0;i < temp_length;i++)
			{
				real_data_out[i+offset_out] = real_data_in[i+offset_in];
				imag_data_out[i+offset_out] = imag_data_in[i+offset_in];
			}

			/* Aanvullen met nullen */
			for (i=temp_length;i < DataLength(vp[out_im]);i++)
			{
				real_data_out[i+offset_out] = 0.0;
				imag_data_out[i+offset_out] = 0.0;
			}
		}
	}

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

	return 0;
}

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

int ShiftCi(char *p)
{
	int in_im,out_im,i,shift;  /* Vensternummer */
	unsigned int alloc_size,chan_offset, channel_length;
	int channel;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);


	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;                /* Signaal bestaat niet */

	shift          = intarg(&p,NULL,"Shift",-DataLength(vp[in_im]),DataLength(vp[in_im]),0);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,32);

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	channel_length = DataRecords(vp[out_im])*DataLength(vp[out_im]);

	for (channel = 0; channel < DataChannels(vp[out_im]);channel++)
	{

		/* Bereken offset van betreffende channel */
		chan_offset = channel*channel_length;

		for (i=0; i < channel_length ;i++)
		{
			if (shift >= 0 )
			{
				if (i  < shift)
				{
					real_data_out[i+chan_offset] = 0;
					imag_data_out[i+chan_offset] = 0;
				}
				if (i >= shift)
				{
					real_data_out[i+chan_offset] = real_data_in[i-shift+chan_offset];
					imag_data_out[i+chan_offset] = imag_data_in[i-shift+chan_offset];
				}
			}
			if (shift < 0)
			{
				if (i  >= channel_length + shift)
				{
					real_data_out[i+chan_offset] = 0;
					imag_data_out[i+chan_offset] = 0;
				}
				if (i  < channel_length + shift)
				{
					real_data_out[i+chan_offset] = real_data_in[i-shift+chan_offset];
					imag_data_out[i+chan_offset] = imag_data_in[i-shift+chan_offset];
				}
			}
		}
	}


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

	return 0;
}


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

int RotateCi(char *p)
{
	int in_im,out_im,i,rotate;  /* Vensternummer */
	unsigned int alloc_size,chan_offset, channel_length;
	int channel;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);


	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;                /* Signaal bestaat niet */

	rotate          = intarg(&p,NULL,"Rotate",-DataLength(vp[in_im]),DataLength(vp[in_im]),0);
	out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);

	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,32);

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	channel_length = DataRecords(vp[out_im])*DataLength(vp[out_im]);

	for (channel = 0; channel < DataChannels(vp[out_im]);channel++)
	{

		/* Bereken offset van betreffende channel */
		chan_offset = channel*channel_length;

		for (i=0;i < channel_length ;i++)
		{
			if (rotate >= 0 )
			{
				if (i  < rotate)
				{
					real_data_out[i+chan_offset] = real_data_in[channel_length - rotate + i + chan_offset];
					imag_data_out[i+chan_offset] = imag_data_in[channel_length - rotate + i + chan_offset];
				}
				if (i >= rotate)
				{
					real_data_out[i+chan_offset] = real_data_in[i-rotate+chan_offset];
					imag_data_out[i+chan_offset] = imag_data_in[i-rotate+chan_offset];
				}
			}
			if (rotate < 0)
			{
				if (i  >= (channel_length + rotate))
				{
					real_data_out[i+chan_offset] = real_data_in[i - (channel_length + rotate) + chan_offset];
					imag_data_out[i+chan_offset] = imag_data_in[i - (channel_length + rotate) + chan_offset];
				}
				if (i  < (channel_length + rotate))
				{
					real_data_out[i+chan_offset] = real_data_in[i-rotate+chan_offset];
					imag_data_out[i+chan_offset] = imag_data_in[i-rotate+chan_offset];
				}
			}
		}
	}


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

	return 0;
}

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

int ClipCi(char *p)
{
	int in_im,out_im,i;  /* Vensternummer */
	int left,right;
	double leftfreq,rightfreq;
	unsigned int alloc_size,offset;
	int channel,record;
	double atten;
	char *in_signalname;
	char *out_signalname;
	char buffer1[128],buffer2[128];
	ASM_SAMPLE *real_data_out;
	ASM_SAMPLE *imag_data_out;
	ASM_SAMPLE *real_data_in;
	ASM_SAMPLE *imag_data_in;

	in_signalname  = strarg(&p,NULL,"Input Signal","a",buffer1);


	in_im = Find_Signal_Venster(in_signalname);
	if (in_im == -1) return ERR_SIGNAL;                /* Signaal bestaat niet */



	if (DataDomain(vp[in_im])==TIME)
	{
		left           = intarg(&p,NULL,"Left (element)",0,DataLength(vp[in_im]),0);
		right          = intarg(&p,NULL,"Rigth (element)",0,DataLength(vp[in_im]),DataLength(vp[in_im]));
		leftfreq       =  1.0;
		rightfreq      =  (DataSampleRate(vp[in_im])/2)*10;
		atten          =  50.0;
		out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);
	}
	else
		if (DataDomain(vp[in_im])==FREQ)
		{
			left           = 0.0;
			right          = DataLength(vp[in_im]);
			leftfreq       = doublearg(&p,NULL,"Left (frequency)",1,(DataSampleRate(vp[in_im])/2)*10,1);
			rightfreq      = doublearg(&p,NULL,"Rigth (frequency)",left,(DataSampleRate(vp[in_im])/2)*10,(DataSampleRate(vp[in_im])/2)*10);
			atten          = (double) intarg(&p,NULL,"Attenuate (dB)",0,100,50);
			out_signalname = strarg(&p,NULL, "Output Signal",in_signalname,buffer2);
		}
		else return ERR_DOMAIN;


	alloc_size = DataAllocSize(vp[in_im]); /* Geheugenruimte grootte nodig voor nieuw venster */

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


	real_data_out = DataReal(vp[out_im]);             /* Bewaar de pointers voor de data */
	imag_data_out = DataImag(vp[out_im]);
	real_data_in  = DataReal(vp[in_im]);
	imag_data_in  = DataImag(vp[in_im]);


	(*vp[out_im]).header = (*vp[in_im]).header;   /* Maak de headers aan elkaar gelijk   */


	DataReal(vp[out_im]) = real_data_out;             /* Zet de pointers van de data weer terug*/
	DataImag(vp[out_im]) = imag_data_out;

	strncpy(DataSignalName(vp[out_im]),out_signalname,32);

	Window_Mode(vp[out_im])= Window_Mode(vp[in_im]);
	Window_Record(vp[out_im])=0;
	Window_Channel(vp[out_im])=0;
	Window_HScale(vp[out_im]) = Window_HScale(vp[in_im]);

	if (DataDomain(vp[in_im]) == TIME)
	{
		for (channel = 0; channel < DataChannels(vp[out_im]);channel++)
		{
			for(record = 0; record < DataRecords(vp[out_im]);record++)
			{
				/* Bereken offset van betreffende record */
				offset = (channel*DataRecords(vp[out_im]) + record)*DataLength(vp[out_im]);

				for (i=0;i < DataLength(vp[out_im]);i++)
				{
					if ( (i < left) || (i > right) )
					{
						real_data_out[i + offset] = 0.0;
						imag_data_out[i + offset] = 0.0;
					}
					else
					{
						real_data_out[i+offset] = real_data_in[i+offset];
						imag_data_out[i+offset] = imag_data_in[i+offset];
					}
				}

			}
		}
	}

	atten=  pow(10,atten/10);
	left = (int)((leftfreq/(10.0*(double)DataSampleRate(vp[in_im])))*(double)DataLength(vp[in_im]));
	right = (int)(  ( rightfreq/(10.0*(double)DataSampleRate(vp[in_im])) )*(double)DataLength(vp[in_im]) );
	if(left < 0.0) left = 0;

	if (DataDomain(vp[in_im])==FREQ)
		for (channel = 0; channel < DataChannels(vp[out_im]);channel++)
		{
			for(record = 0; record < DataRecords(vp[out_im]);record++)
			{
				/* Bereken offset van betreffende record */
				offset = (channel*DataRecords(vp[out_im]) + record)*DataLength(vp[out_im]);

				for (i=0;i < DataLength(vp[out_im]);i++)
				{
					if (   (i < left)
					    || ( (i > right) && (i < (DataLength(vp[out_im]) - right -1)))
					    || (i > (DataLength(vp[out_im]) - left -1) ) )
					{
						real_data_out[i + offset] = real_data_in[i + offset]/atten;
						imag_data_out[i + offset] = imag_data_in[i + offset]/atten;
					}
					else
					{
						real_data_out[i+offset] = real_data_in[i+offset];
						imag_data_out[i+offset] = imag_data_in[i+offset];
					}
				}

			}
		}



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

	return 0;
}
