Dual DIP-switched IIR Filter
//iirFilterSwitch.c
// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#include "IIRHPF.h"
// The intermediate values in the Direct Form II filter
float delay_w1[MWSPT_NSEC1][3];
float delay_w2[MWSPT_NSEC2][3];
// delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function
{
short i; // i loops through the MWSPT_NSEC number of sections
// Use the LPF if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, we read in the x-value, apply the first stage gain
sectionOut = NUM1[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC1; i++) { // Loop through all the sections
// Get the new delay_w1[0];
delay_w1[i][0] = sectionOut - DEN1[i][1]*delay_w1[i][1] - DEN1[i][2]*delay_w1[i][2];
// Get the output of this section
sectionOut = NUM1[i][0]*delay_w1[i][0] + NUM1[i][1]*delay_w1[i][1] + NUM1[i][2]*delay_w1[i][2];
// Delay the w's for the next interrupt
delay_w1[i][2] = delay_w1[i][1];
delay_w1[i][1] = delay_w1[i][0];
}
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then use HPF
sectionOut = NUM2[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC2; i++) { // Loop through all the sections
// Get the new delay_w2[0];
delay_w2[i][0] = sectionOut - DEN2[i][1]*delay_w2[i][1] - DEN2[i][2]*delay_w2[i][2];
// Get the output of this section
sectionOut = NUM2[i][0]*delay_w2[i][0] + NUM2[i][1]*delay_w2[i][1] + NUM2[i][2]*delay_w2[i][2];
// Delay the w's for the next interrupt
delay_w2[i][2] = delay_w2[i][1];
delay_w2[i][1] = delay_w2[i][0];
}
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
}
return; // return from interrupt
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC1; i++)
for (j=0; j<3; j++)
delay_w1[i][j] = 0; // init intermediate array
for (i=0; i<MWSPT_NSEC2; i++)
for (j=0; j<3; j++)
delay_w2[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}
IIR FIlter and add tone
//iirFilter.c
// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#define A 1 //The amplitude of added wave
#define FREQ 1000 //The frequency of the sine wave in Hz
// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3];
// delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function t=125us, f = 8kHz
{
short i; // i loops through the MWSPT_NSEC number of sections
int period = 8000 / FREQ;
float rad = FREQ * 2 * pi;
int j = 0;
// Do the filtering if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, we read in the x-value, apply the first stage gain
sectionOut = NUM[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
// Get the new delay_w[0];
delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];
// Get the output of this section
sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
// Delay the w's for the next interrupt
delay_w[i][2] = delay_w[i][1];
delay_w[i][1] = delay_w[i][0];
}
//Add a tone to sectionOut
sectionOut = sectionOut + A*sin(rad*j); //Add a sine wave of freq rad to sectionOut
j = (j + 1) % period; //Increment the sine wave counter
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; // return from interrupt
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC; i++)
for (j=0; j<3; j++)
delay_w[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}
IIR Bandstop Filter
// Include the filter coefficients and corresponding variables
#include "IIRBSF.h"
// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3]; // delay_w[i][j] <=> w_i(n-j), j=0,1,2
// i is the section, j the delay
float sectionOut; // yk[n] <=> sectionout
interrupt void isr() //Interrupt function t=125us, f = 8kHz
{
short i; // i loops through the MWSPT_NSEC number of sections
// Do the filtering if DIP switch 1 up
if (get_DIP1() == 1) {
// In the first section, read in the x-value, apply the first stage gain
sectionOut = NUM[0][0] * get_sample();
for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
// Get the new delay_w[0];
delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];
// Get the output of this section
sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
// Delay the w's for the next interrupt
delay_w[i][2] = delay_w[i][1];
delay_w[i][1] = delay_w[i][0];
}
// Apply the gain, convert to short and send out
send_output((short)(2 * sectionOut));
// Gain of 2 chosen heuristically for speech from PC
} else { // If DIP switch 1 down, == 0, then just pass through signal.
send_output(get_sample());
}
return; //interrupt done
}
void main()
{
short i,j;
for (i=0; i<MWSPT_NSEC; i++)
for (j=0; j<3; j++)
delay_w[i][j] = 0; // init intermediate array
init_all(); // init all
while(1); // infinite loop
}
Correlation on DSP KIT TMS320C6713 Simulator
#include <stdio.h>
#include <math.h>
double pi=3.14159;
float x[50],y[50];
float r[100];
void correlation(float *x,float *y, int nx, int ny);
int main()
{
int ii;
for(ii=0;ii<50 ;ii++)
{
x[ii]=sin(2.0*pi*ii*3/50);
y[ii]=sin(2.0*pi*ii*3/50);
}
correlation(x,y,50,50);
printf("Complete.\n");
return 0;
}
void correlation(float *x,float *y, int nx, int ny)
{
int n=50,delay=0,maxdelay=50;
int i,j;
double mx,my,sx,sy,sxy,denom;
/* Calculate the mean of the two series x[], y[] */
mx = 0;
my = 0;
for (i=0;i<n;i++)
{
mx += x[i];
my += y[i];
}
mx /= n;
my /= n;
/* Calculate the denominator */
sx = 0;
sy = 0;
for (i=0;i<n;i++)
{
sx += (x[i] - mx) * (x[i] - mx);
sy += (y[i] - my) * (y[i] - my);
}
denom = sqrt(sx*sy);
/* Calculate the correlation series */
for (delay=-maxdelay;delay<maxdelay;delay++)
{
sxy = 0;
for (i=0;i<n;i++)
{
j = i + delay;
if (j < 0 || j >= n)
continue;
else
sxy += (x[i] - mx) * (y[j] - my);
// Or should it be (?)
/* if (j < 0 || j >= n)
sxy += (x[i] - mx) * (-my);
else
sxy += (x[i] - mx) * (y[j] - my);*/
}
r[delay+maxdelay]= ( sxy / denom);
Constant complexity rectangular QAM demodulator
% N is the size of the constellation.
% v is the input data.
% For details email me.
void function_qam()
{
co=0;
m=0;
for(q=0; q<N; q++)
{
a=v[m];// a holds the real part
b=v[m+1];// b holds the imaginary part
d=a-onern[max]; %onern stores the negative real axis points and onein stores the negative imag axis points. eg for 256 QAM, each will store 0 to -8 as an array.
l=d; //l stores the distance
s=(d-l);
if(l<max-1) % max is the maximum value that the 1-d coordinate can take.
{
if(d>0){
if(s<=.5)
{
hi=max-l;
flagtwo=1;
}
else
{
hi=max-l-1;
flagtwo=1;
}
}
else
{
hi=max;
flagtwo=1;
}
}
if(l==max-1)
{
hi=1;
flagtwo=1;
}
if(l==max)//
{
flagone=1;
hi=1;
}
if(l>max)
{
if(d<2*max)
{
if(s<=.5)
{
hi=l-max;
flagone=1;
}
else
{
hi=l-max+1;
flagone=1;
}
}
else
{
hi=max;
flagone=1;
}
}
de=b-onein[max];
le=de; //l stores the distance
se=(de-le);
if(le<max-1)
{
if(de>0)
{
if(se<=.5)
{
hm=max-le;
flagfour=1;
}
else
{
hm=max-le-1;
flagfour=1;
}
}
else
{
hm=max;
flagfour=1;
}
}
if(le==max-1)
{
hm=1;
flagfour=1;
}
if(le==max)
{
flagthree=1;
hm=1;
}
if(le>max)
{
if(de<2*max){
if(se<=.5)
{
hm=le-max;
flagthree=1;
}
else
{
hm=le-max+1;
flagthree=1;
}
}
else
{
hm=max;
flagthree=1;
}
}
if(flagone==1)
{
vv[m]=hi; // now v[m] holds one x coordinate of the qam mapping and v[m+1] holds the y coordinate
}
else
{
vv[m]= 100-hi;
}
if(flagthree==1)
{
vv[m+1]=hm;
}
else
{
vv[m+1]= 100-hm;
}
flagone=0;
flagtwo=0;
flagthree=0;
flagfour=0;
r=0;
output[co]= valuetable[ vv[m] ][ vv[m+1] ] ;
m = m+ 2;
co = co+ 1;
}
for(t=0;t<(N/2);t++)
{
p[t] = (output[t]);
}
}
Using EMIF expansion port as digital output
/* David Valencia at DSPRelated.com
This example shows how to use the External Memory Interface
expansion port of Spectrum Digital DSK6713 card as
a set of digital imputs
The use of buffer ICs is mandatory, as these pins can't deliver
too much current */
#define OUTPUT 0xA0000000 // EMIF ADDRESS (updated 3/11/2010)
int *output = (int*)OUTPUT; // Declare a pointer to EMIF's address
void main()
{
// To see how the value is written to the EMIF pins,
// you may prefer to run the program step-by-step
// Write a 32-bit digital value to the pins
*output = 0x48120A00; // Test value
}