//! The main function int main(int argc, char *argv[]) { dsp32_t vect1[SIZE]; int cycle_count; int frequency, sample_rate; dsp32_t phase, amplitude; // Switch to external Oscillator 0. pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize the DSP debug module dsp_debug_initialization(FOSC0); // 1 KHz frequency = 1000; // 10 KHz sample_rate = 100000; // phase = PI/2 phase = DSP32_Q(0.5); // amplitude amplitude = DSP32_Q(1.); dsp32_debug_printf("32-bit signal generation program test\n"); // Compute the signal to generate switch(SIGNAL_TO_USE) { // Sinusoidal case SIGNAL_SIN: dsp32_debug_printf("Sine\n"); dsp32_debug_printf("Frequency: %d, Fs: %d, Phase: %f\n", frequency, sample_rate, phase); cycle_count = Get_sys_count(); dsp32_gen_sin(vect1, SIZE, frequency, sample_rate, phase); cycle_count = Get_sys_count() - cycle_count; break; // Cosinusoidal case SIGNAL_COS: dsp32_debug_printf("Cosine\n"); dsp32_debug_printf("Frequency: %d, Fs: %d, Phase: %f\n", frequency, sample_rate, phase); cycle_count = Get_sys_count(); dsp32_gen_cos(vect1, SIZE, frequency, sample_rate, phase); cycle_count = Get_sys_count() - cycle_count; break; // Noise case SIGNAL_NOISE: dsp32_debug_printf("Noise\n"); dsp32_debug_printf("Amplitude: %d\n", amplitude); cycle_count = Get_sys_count(); dsp32_gen_noise(vect1, SIZE, amplitude); cycle_count = Get_sys_count() - cycle_count; break; } // Print the number of cycles dsp32_debug_printf("Cycle count: %d\n", cycle_count); // Print the signal dsp32_debug_print_vect(vect1, SIZE); while(1); }
void dsp32_gen_dcomb(dsp32_t *vect1, int size, int f, int fs, dsp32_t delay) { int i; int t_inc, t_low, cp; int t_delay; // Number of sample per period t_inc = fs / f; // Number of sample for the delay t_delay = (((S64) delay) * ((S64) t_inc)) >> DSP32_QB; // Number of sample per low signal t_inc--; i = 0; t_low = t_delay; // Main loop while(i < size) { // Low part cp = MIN(t_low, size); for(; i<cp; i++) vect1[i] = DSP32_Q(0.); // High part if (i < size) vect1[i++] = DSP32_Q(1.); t_low = t_inc + i; } }
void dsp32_gen_rect(dsp32_t *vect1, int size, int f, int fs, dsp32_t duty, dsp32_t delay) { int i = 0; int t_low_inc, t_high_inc, cp; int t_low, t_high, t_delay; // Number of sample per period t_low_inc = fs / f; // Number of sample per high signal t_high_inc = (((S64) duty) * ((S64) t_low_inc)) >> DSP32_QB; // Number of sample for the delay t_delay = (((S64) delay) * ((S64) t_low_inc)) >> DSP32_QB; // Number of sample per low signal t_low_inc -= t_high_inc; i = 0; // For the delay t_high = MIN(t_high_inc, t_delay - t_low_inc); t_low = MIN(t_low_inc, t_delay); if (t_high > 0) t_low += t_high; // Main loop while(i < size) { // High part cp = MIN(t_high, size); for(; i<cp; i++) vect1[i] = DSP32_Q(1.); // Low part cp = MIN(t_low, size); for(; i<cp; i++) vect1[i] = DSP32_Q(-1.); t_high = t_high_inc + i; t_low = t_high + t_low_inc; } }
dsp32_t dsp32_gen_saw(dsp32_t *vect1, int size, int f, int fs, dsp32_t duty, dsp32_t delay) { int i = 0; S32 t_low_inc, t_high_inc, t; dsp32_t delta_rise, delta_decrease, delta; // Number of sample per period t_low_inc = fs / f; // Number of sample per high signal t_high_inc = (((S64) duty) * ((S64) t_low_inc)) >> DSP32_QB; // Number of sample for the delay t = (((S64) delay) * ((S64) t_low_inc)) >> DSP32_QB; // Number of sample per low signal t_low_inc -= t_high_inc; // Calculate the delta coefficient of the rising edge of the saw tooth delta_rise = (DSP32_Q(1.) / t_high_inc) * 2; // Calculate the delta coefficient of the decreasing edge of the saw tooth delta_decrease = (DSP32_Q(1.) / t_low_inc) * 2; // Compute the initial value if (t < t_high_inc) delta = DSP32_Q(-1.) + t * delta_rise; else delta = DSP32_Q(1.) - (t - t_high_inc) * delta_decrease; while(i < size) { int lim, j; if (i) t = 0; lim = Max(0, Min(t_high_inc - t, size - i)); for(j=0; j<lim; j++) vect1[i++] = (delta += delta_rise); t += lim; if (j) delta = DSP32_Q(1.); lim = Min(t_high_inc + t_low_inc - t, size - i); for(j=0; j<lim; j++) vect1[i++] = (delta -= delta_decrease); t += lim; delta = DSP32_Q(-1.); } return (t << DSP32_QB) / (t_high_inc + t_low_inc); }
#include "sysclk.h" #include "cycle_counter.h" // Length of the output signal #define VECT1_SIZE 16 // Length of the first input signal #define VECT2_SIZE 16 // Length of the second input signal #define VECT3_SIZE 16 // The output signal A_ALIGNED dsp32_t vect1[VECT1_SIZE]; // The first input signal A_ALIGNED dsp32_t vect2[VECT2_SIZE] = { DSP32_Q(0.012010), DSP32_Q(0.059717), DSP32_Q(0.101397), DSP32_Q(0.0583150), DSP32_Q(0.0000000), DSP32_Q(0.0921177), DSP32_Q(0.0951057), DSP32_Q(0.0884270), DSP32_Q(0.0732020), DSP32_Q(0.515080), DSP32_Q(0.0583150) }; // The second input signal A_ALIGNED dsp32_t vect3[VECT3_SIZE] = { DSP32_Q(0.5), DSP32_Q(0.101397),
#include "dsp_debug.h" #include "pm.h" #include "cycle_counter.h" //! The size of the input signal #define VECT2_SIZE 64 //! The number of tap #define VECT3_SIZE 25 //! The output buffer A_ALIGNED dsp32_t vect1[VECT2_SIZE + 2*(VECT3_SIZE - 1) + 4]; // <- "+4" for avr32-uc3 optimized FIR version //! First input signal A_ALIGNED dsp32_t vect2[VECT2_SIZE] = { DSP32_Q(0.00000000000), DSP32_Q(0.05971733275), DSP32_Q(0.10139671590), DSP32_Q(0.11013997328), DSP32_Q(0.07684940150), DSP32_Q(0.00000000009), DSP32_Q(-0.11375674268), DSP32_Q(-0.25026678813), DSP32_Q(-0.38974690925), DSP32_Q(-0.50960156480), DSP32_Q(-0.58778525220), DSP32_Q(-0.60622623913), DSP32_Q(-0.55381024235), DSP32_Q(-0.42847700897), DSP32_Q(-0.23810168698), DSP32_Q(-0.00000000072),
#include "sysclk.h" #include "cycle_counter.h" // The size of the signal #define SIZE 32 // log2(SIZE) #define NLOG 6 // The output buffer A_ALIGNED dsp32_complex_t vect1[SIZE]; A_ALIGNED dsp32_t fft_real[SIZE]; // Input signal resulting from a multiplication between a cosine and a sine. A_ALIGNED dsp32_t vect2[SIZE] = { DSP32_Q(0.0000000000), DSP32_Q(0.0597173328), DSP32_Q(0.1013967159), DSP32_Q(0.1101399733), DSP32_Q(0.0768494015), DSP32_Q(0.0000000000), DSP32_Q(-0.1137567428), DSP32_Q(-0.2502667883), DSP32_Q(-0.3897469095), DSP32_Q(-0.5096015650), DSP32_Q(-0.5877852523), DSP32_Q(-0.6062262391), DSP32_Q(-0.5538102421), DSP32_Q(-0.4284770086), DSP32_Q(-0.2381016864), DSP32_Q(-0.0000000000),
#include "compiler.h" #include "board.h" #include "dsp.h" #include "dsp_debug.h" #include "pm.h" #include "cycle_counter.h" #include "gpio.h" //! \brief The size of the input signal #define SIZE 64 //! \brief Buffer containing input data A_ALIGNED dsp32_t input_x[SIZE] = { DSP32_Q(0.00000000000), DSP32_Q(0.03718075139), DSP32_Q(0.07131184859), DSP32_Q(0.09963983090), DSP32_Q(0.11997464035), DSP32_Q(0.13090169944), DSP32_Q(0.13191810690), DSP32_Q(0.12347962859), DSP32_Q(0.10695389264), DSP32_Q(0.08448437894), DSP32_Q(0.05877852523), DSP32_Q(0.03284069954), DSP32_Q(0.00967618536), DSP32_Q(-0.00800483670), DSP32_Q(-0.01805432735), DSP32_Q(-0.01909830056),
void svpwm_calc(volatile svpwm_options_t *svpwm_options,U8 dir) { volatile U32 dx,dy,div; volatile U32 T; div = dsp32_op_div(svpwm_options->teta,svpwm_options->resolution); dy = dsp32_op_sin(dsp32_op_mul(DSP32_Q(1./3.),div)); dy = (((U64)(svpwm_options->amplitude))*((U64)(dy)))>>DSP32_QB; div = dsp32_op_div(svpwm_options->resolution-svpwm_options->teta,svpwm_options->resolution); dx = dsp32_op_sin(dsp32_op_mul(DSP32_Q(1./3.),div)); dx = (((U64)(svpwm_options->amplitude))*((U64)(dx)))>>DSP32_QB; T = svpwm_options->max_frequency / 2; if (dir==0) { switch(svpwm_options->sector_number) { case HS_001 : svpwm_options->PWM0 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T + dx/2 - dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T + dx/2 + dy/2) ; break ; case HS_101 : svpwm_options->PWM0 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T - dx/2 + dy/2) ; break ; case HS_100 : svpwm_options->PWM0 = (unsigned short int) (T + dx/2 - dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T - dx/2 - dy/2) ; break ; case HS_110 : svpwm_options->PWM0 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T - dx/2 + dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T - dx/2 - dy/2) ; break ; case HS_010 : svpwm_options->PWM0 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T + dx/2 - dy/2) ; break ; case HS_011 : svpwm_options->PWM0 = (unsigned short int) (T - dx/2 + dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T + dx/2 + dy/2) ; break ; default : svpwm_options->PWM0 = (unsigned short int) (T) ; svpwm_options->PWM1 = (unsigned short int) (T) ; svpwm_options->PWM2 = (unsigned short int) (T) ; break ; } } else { switch(svpwm_options->sector_number) { case HS_110 : svpwm_options->PWM0 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T + dx/2 - dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T + dx/2 + dy/2) ; break ; case HS_010 : svpwm_options->PWM0 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T - dx/2 + dy/2) ; break ; case HS_011 : svpwm_options->PWM0 = (unsigned short int) (T + dx/2 - dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T - dx/2 - dy/2) ; break ; case HS_001 : svpwm_options->PWM0 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T - dx/2 + dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T - dx/2 - dy/2) ; break ; case HS_101 : svpwm_options->PWM0 = (unsigned short int) (T + dx/2 + dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T + dx/2 - dy/2) ; break ; case HS_100 : svpwm_options->PWM0 = (unsigned short int) (T - dx/2 + dy/2) ; svpwm_options->PWM1 = (unsigned short int) (T - dx/2 - dy/2) ; svpwm_options->PWM2 = (unsigned short int) (T + dx/2 + dy/2) ; break ; default : svpwm_options->PWM0 = (unsigned short int) (T) ; svpwm_options->PWM1 = (unsigned short int) (T) ; svpwm_options->PWM2 = (unsigned short int) (T) ; break ; } } }
//! The main function int main(int argc, char *argv[]) { dsp32_t number, result; int cycle_count; char sin_result[50], cos_result[50], asin_result[50], acos_result[50], rand_result[50], sqrt_result[50], abs_result[50], ln_result[50], log2_result[50], log10_result[50], exp_result[50], pow_result[50]; /** * \note the call to sysclk_init() will disable all non-vital * peripheral clocks, except for the peripheral clocks explicitly * enabled in conf_clock.h. */ sysclk_init(); // Initialize the DSP debug USART module when running in external board #if BOARD != AVR_SIMULATOR_UC3 dsp_debug_initialization(FOSC0); #endif number = DSP32_Q(NUMBER_TO_COMPUTE); /* * Place a breakpoint on sprintf and check the ASCII output in * %operator%_result in Watch Window or Memory Window. * Note: Edit the variable name in Watch Window and append * "&" at the start and ",s" at the end for displaying string. * Read Watch Window Format Specifiers for more info. */ // 32-bit fixed point cosine cycle_count = Get_sys_count(); result = dsp32_op_cos(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(cos_result, "cos(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point sine cycle_count = Get_sys_count(); result = dsp32_op_sin(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(sin_result, "sin(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point arc cosine cycle_count = Get_sys_count(); result = dsp32_op_acos(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(acos_result, "acos(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point arc sine cycle_count = Get_sys_count(); result = dsp32_op_asin(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(asin_result, "asin(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point random dsp_op_srand(number); cycle_count = Get_sys_count(); result = dsp32_op_rand(); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(rand_result, "rand() %f (%i cycles)\n", result, cycle_count); // 32-bit fixed point square root cycle_count = Get_sys_count(); result = dsp32_op_sqrt(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(sqrt_result, "sqrt(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point absolute cycle_count = Get_sys_count(); result = dsp32_op_abs(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(abs_result, "abs(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point natural logarithm cycle_count = Get_sys_count(); result = dsp32_op_ln(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(ln_result, "ln(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point binary logarithm cycle_count = Get_sys_count(); result = dsp32_op_log2(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(log2_result, "log2(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point common logarithm cycle_count = Get_sys_count(); result = dsp32_op_log10(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(log10_result, "log10(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point exponential cycle_count = Get_sys_count(); result = dsp32_op_exp(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(exp_result, "exp(%f) %f (%i cycles)\n", number, result, cycle_count); // 32-bit fixed point power cycle_count = Get_sys_count(); result = dsp32_op_pow(DSP32_Q(0.), number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_sprintf(pow_result, "pow(0.5, %f) %f (%i cycles)\n", number, result, cycle_count); while (1) { // Intentionally left blank. } }
//! The size of the input signal #define SIZE 48 // A High-pass IIR Filter // Butterworth model // Fsample = 48 KHz // Fcut-off = 2000 Hz // Order 5 #define NUM_SIZE 6 #define NUM_PREDIV 3 #define DEN_SIZE 5 #define DEN_PREDIV 3 A_ALIGNED dsp32_t num[NUM_SIZE] = { DSP32_Q(0.6537018 / (1 << NUM_PREDIV)), DSP32_Q(-3.2685088 / (1 << NUM_PREDIV)), DSP32_Q(6.5370176 / (1 << NUM_PREDIV)), DSP32_Q(-6.5370176 / (1 << NUM_PREDIV)), DSP32_Q(3.2685088 / (1 << NUM_PREDIV)), DSP32_Q(-0.6537018 / (1 << NUM_PREDIV)) }; A_ALIGNED dsp32_t den[DEN_SIZE] = { DSP32_Q(-4.1534907 / (1 << DEN_PREDIV)), DSP32_Q(6.9612765 / (1 << DEN_PREDIV)), DSP32_Q(-5.877997 / (1 << DEN_PREDIV)), DSP32_Q(2.498365 / (1 << DEN_PREDIV)), DSP32_Q(-0.427326 / (1 << DEN_PREDIV)) };
#define SIZE 144 // A Low-pass IIR Filter // Butterworth model // Fsample = 8 KHz // Fcut-off = 2000 Hz // Order 18 #define NUM_SIZE 19 #define DEN_SIZE 18 #define PREDIV 2 A_ALIGNED dsp32_t num[NUM_SIZE] = { DSP32_Q(0.0000274/(1 << PREDIV)), DSP32_Q(0.0004939/(1 << PREDIV)), DSP32_Q(0.0041985/(1 << PREDIV)), DSP32_Q(0.0223922/(1 << PREDIV)), DSP32_Q(0.0839706/(1 << PREDIV)), DSP32_Q(0.2351178/(1 << PREDIV)), DSP32_Q(0.5094219/(1 << PREDIV)), DSP32_Q(0.8732946/(1 << PREDIV)), DSP32_Q(1.2007801/(1 << PREDIV)), DSP32_Q(1.3342001/(1 << PREDIV)), DSP32_Q(1.2007801/(1 << PREDIV)), DSP32_Q(0.8732946/(1 << PREDIV)), DSP32_Q(0.5094219/(1 << PREDIV)), DSP32_Q(0.2351178/(1 << PREDIV)), DSP32_Q(0.0839706/(1 << PREDIV)), DSP32_Q(0.0223922/(1 << PREDIV)),
//! The main function int main(int argc, char *argv[]) { dsp32_t number, result; int cycle_count; // Switch to external Oscillator 0. pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize the DSP debug module dsp_debug_initialization(FOSC0); dsp32_debug_printf("32-bit fixed point operators program test\n"); dsp32_debug_printf("Type a number: "); number = dsp_debug_read_q(DSP32_QA, DSP32_QB); dsp32_debug_printf("Number to compute: %f\n", number); // 32-bit fixed point cosine cycle_count = Get_sys_count(); result = dsp32_op_cos(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("cos(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point sine cycle_count = Get_sys_count(); result = dsp32_op_sin(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("sin(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point arc cosine cycle_count = Get_sys_count(); result = dsp32_op_acos(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("acos(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point arc sine cycle_count = Get_sys_count(); result = dsp32_op_asin(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("asin(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point random dsp_op_srand(number); cycle_count = Get_sys_count(); result = dsp32_op_rand(); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("rand()\t\t\t\t%f\t(%i cycles)\n", result, cycle_count); // 32-bit fixed point square root cycle_count = Get_sys_count(); result = dsp32_op_sqrt(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("sqrt(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point absolute cycle_count = Get_sys_count(); result = dsp32_op_abs(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("abs(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point natural logarithm cycle_count = Get_sys_count(); result = dsp32_op_ln(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("ln(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point binary logarithm cycle_count = Get_sys_count(); result = dsp32_op_log2(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("log2(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point common logarithm cycle_count = Get_sys_count(); result = dsp32_op_log10(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("log10(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point exponential cycle_count = Get_sys_count(); result = dsp32_op_exp(number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("exp(%f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); // 32-bit fixed point power cycle_count = Get_sys_count(); result = dsp32_op_pow(DSP32_Q(0.), number); cycle_count = Get_sys_count() - cycle_count; dsp32_debug_printf("pow(0.5, %f)\t\t%f\t(%i cycles)\n", number, result, cycle_count); while(1); }