Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
LUTS.cpp
00001 #include "mbed.h" 00002 #include "LUTs.h" 00003 00004 LUTs::LUTs() 00005 { 00006 } 00007 LUTs::~LUTs() 00008 { 00009 } 00010 volatile uint16_t sin_wavtable[1024]; 00011 volatile uint16_t tri_wavtable[1024]; 00012 volatile uint16_t pulse_wavtable[1024]; 00013 00014 void LUTs::initial_wavetables() 00015 { 00016 sin_wavetable(); 00017 tri_wavetable(50); 00018 pulse_wavetable(50); 00019 } 00020 00021 void LUTs::sin_wavetable() 00022 { 00023 //printf("Generating sin Wavetable \n"); 00024 for (int i=0; i<1024; i++) { //Size of table 00025 sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5); //Genrate Value 00026 rem= fmod(sin_d,1); //calculates remainder for rounding 00027 //printf("preround= %g -", sin_d); //DEBUG 00028 if (rem>=0.5) { 00029 sin_d=ceil(sin_d); //round UP 00030 } else { 00031 sin_d= floor(sin_d-rem); //round DOWN 00032 } 00033 // printf("Postround= %g -",sin_d); //DEBUG 00034 sin_u=((uint16_t)sin_d); //Value converted to unsigned 00035 //printf("sin_u= %u \n", sin_u); 00036 sin_wavtable[i]=sin_u;//Sets in Global Variable 00037 //sin_wavtable[i]=get_sin(i); 00038 } 00039 #ifdef SLOW_TIME 00040 for (int i=0; i<1024; i=i+128) { 00041 printf("sin_wav[%d]= %u \n", i, sin_wavtable); //prints Key values 00042 } 00043 #endif 00044 } 00045 00046 void LUTs::tri_wavetable(int pulsewidth) 00047 { 00048 //printf("Generating Tri-wavetable\n"); 00049 tri_wavtable[0]=0; //sets 0 value 00050 rise_t=(pulsewidth*1024/100); //Calculates rise samples 00051 rise_tu=(uint16_t)rise_t; 00052 fall_tu=1024-rise_tu; //Calculates fall samples 00053 dif=65536/rise_t; //Calculates Rise Dif 00054 dif_u=(uint16_t)dif; //converts to uint 00055 00056 #ifdef SLOW_TIME //Prints Values in SLOW_TIME case 00057 printf("PRINTING TRI WAVETABLE Values\n"); 00058 printf("PW= %d \n",pulsewidth); 00059 printf("Rise Samples= %u\n", rise_tu); 00060 printf("Fall Samples= %u\n", fall_tu); 00061 printf("UP sample dif= %u\n", dif_u); 00062 #endif 00063 for (int i=1; i<=rise_tu; i++) { //Generates Rise Points 00064 tri_wavtable[i]=tri_wavtable[i-1]+dif_u; 00065 } 00066 dif=65536/fall_tu; //Calcualtes Fall Diff 00067 dif_u=(uint16_t)dif; 00068 00069 #ifdef SLOW_TIME 00070 printf("down sample dif= %u\n", dif_u); 00071 #endif 00072 for (int i=rise_tu; i<1024; i++) { //Generates Fall Points 00073 tri_wavtable[i]=65535-((i-rise_tu)*dif_u); 00074 } 00075 #ifdef SLOW_TIME 00076 tri_wav_results();//Prints Key Results 00077 #endif 00078 } 00079 00080 00081 void LUTs::pulse_wavetable(int pulsewidth) 00082 { 00083 //printf("Generating Pulse-wavetable\n"); 00084 up_t=(pulsewidth*1024/100); //Samples Up 00085 up_tu=(uint16_t)up_t; 00086 for (int i=0; i<=up_tu; i++) { //Until Down 00087 pulse_wavtable[i]=58981; //Headroom to normalise Volume 00088 //printf("up itt = %d\n",i); 00089 } 00090 00091 for (int i=up_tu+1; i<1024; i++) { //Until Over 00092 pulse_wavtable[i]=6553; //HEadroom Included 00093 //printf("down itt = %d\n",i); 00094 } 00095 } 00096 00097 void LUTs::tri_wav_results() { //Prints Results 00098 printf("TRI_WAV_RESULTS:\n"); 00099 printf("i,out\n"); 00100 for (i=0; i<=1024; i=i+64) { 00101 printf("%d, %u\n",i,tri_wavtable[i]); 00102 } 00103 }
Generated on Wed Jul 13 2022 21:50:34 by
