#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define PI 3.14159265
/* work only with one channel */

void squarewave(int lenght, int end)
{
			int cnt;
			for (cnt = 0 ;cnt<end; cnt++)
			{
				if (cnt < lenght/2)
				{
					printf("0.999 ");
				}
				else
				{
					printf("-0.999 ");
				}
			}
}

void sawtooth(int lenght, int end)
{
			float cnt;
			//for (cnt = -0.999 ;cnt<0.999; cnt += 2.0/(float)lenght)
			//{
			//	printf("%f ",cnt);
			//}
			for (cnt = 0; cnt<end; cnt++)
			{
				printf("%f ",(2.0*((float)cnt)/lenght)-1.0);
			}
}

void reversesawtooth(int lenght, int end)
{
			float cnt;
			//for (cnt = 0.999 ;cnt>-0.999; cnt -= 2.0/(float)lenght)
			//{
			//	printf("%f ",cnt);
			//}
			for (cnt = end; cnt>0; cnt--)
			{
				printf("%f ",(2.0*((float)cnt)/lenght)-1.0);
			}
}

void triangle(int lenght, int end)
{
			int firsthalf = (lenght/2)+(lenght%2) ;
			int secondhalf = (lenght/2);
			if (end > firsthalf)
			{
				sawtooth(firsthalf,firsthalf);
				reversesawtooth(secondhalf,end-firsthalf);
			}
			if (end <= firsthalf)
			{
				sawtooth(firsthalf,end);
			}
}

void noise(int lenght, int end)
{
	srand(time(NULL));
	
			int cnt;
			for (cnt = 0 ;cnt<end; cnt++)
			{
				printf("%f ",((float)rand())/RAND_MAX);
			}
}

void sinusoidal(int lenght, int end)
{
			int cnt;
			for (cnt = 0 ;cnt<end; cnt++)
			{
				printf("%f ",sin(cnt*PI/(lenght/2)));
			}
}

void oscilliate(char * func ,int lenght, int end)
{
				if(!strcmp(func,"square"))
				{
					squarewave(lenght,end);
				}
				if(!strcmp(func,"sawtooth"))
				{
					sawtooth(lenght,end);
				}
				if(!strcmp(func,"reversesawtooth"))
				{
					reversesawtooth(lenght,end);
				}
				if(!strcmp(func,"triangle"))
				{
					triangle(lenght,end);
				}
				if(!strcmp(func,"sinusoidal"))
				{
					sinusoidal(lenght,end);
				}
				if(!strcmp(func,"noise"))
				{
					noise(lenght,end);
				}
}

int main(int argc, char * argv[])
{
	char c;
	char wordv[80];
	int wordc = 0;
	
	while (c != EOF)
	{
		c = getchar();
		if ((c != ' ')&&(c != '\n'))
		{
			wordv[wordc] = c;
			wordc++;
		}
		else
		{
			wordc = 0;
			int lenght = atoi(wordv);
			int end = lenght;
			
			if (argc == 2)
			{
				oscilliate(argv[1], lenght, lenght);
			}
			if (argc == 3)
			{
				end = atoi(argv[2]);
				int repeat = end / lenght;
				int last = end - (repeat*lenght);
				for (repeat=repeat;repeat>0;repeat--)
				{
					oscilliate(argv[1], lenght, lenght);
				}
				oscilliate(argv[1],lenght,last);
			}

			int clear;
			for (clear = 0; clear < 80 ; clear++)
			{
				wordv[clear] = ' ';
			}
		}
	}

	return(0);
}
