Skip to content
Browse files

Added LMS example

Signed-off-by: Mikael <micke.lilltroll@hotmail.com>
  • Loading branch information...
1 parent 419c2d0 commit cf55f4f66f2d137aae2019949daf5469a329899f @lilltroll77 lilltroll77 committed Oct 31, 2011
Showing with 154 additions and 3 deletions.
  1. +4 −1 README
  2. +2 −2 app_lms/Makefile
  3. +94 −0 app_lms/src/main.xc
  4. +4 −0 module_lms/src/lms.h
  5. +50 −0 module_lms/src/lms.xc
View
5 README
@@ -1 +1,4 @@
-This repo is not ready for build
+SC_ADAPTIVE FILTERS
+
+TODO:
+Basic Doc
View
4 app_lms/Makefile
@@ -1,15 +1,15 @@
# The TARGET variable determines what target system the application is
# compiled for. It either refers to an XN file in the source directories
# or a valid argument for the --target option when compiling
-TARGET =
+TARGET = XDK
# The APP_NAME variable determines the name of the final .xe file. It should
# not include the .xe postfix. If left blank the name will default to
# the project name
APP_NAME =
# The USED_MODULES variable lists other module used by the application.
-USED_MODULES =
+USED_MODULES = module_lms module_fir
# The flags passed to xcc when building the application
# You can also set the following to override flags for a particular language:
View
94 app_lms/src/main.xc
@@ -0,0 +1,94 @@
+// Copyright (c) 2011, Mikael Bohman, All rights reserved
+// This software is freely distributable under a derivative of the
+// University of Illinois/NCSA Open Source License posted in
+// LICENSE.txt and at <http://github.xcore.com/>
+
+#include <platform.h>
+#include <xs1.h>
+#include <print.h>
+#include "lms.h"
+
+#define CLOCKDIV 50
+port p_TXD = PORT_UART_TXD;
+clock clk = XS1_CLKBLK_1;
+
+int main() {
+#define ELEMENTS 256
+#define REDUCTION 1
+#define POLYNOMIAL 0xEDB88320
+#define LEN ELEMENTS*48*REDUCTION
+
+ int errorvec[1 + LEN / REDUCTION];
+ int state[2][ELEMENTS];
+ int hreal[ELEMENTS];
+ int hest[ELEMENTS];
+ int hi = 0;
+ int x_des, x_ref, error, noise;
+ unsigned usign1, usign2, seed1, seed2, lo = 0;
+ unsigned POW[2] = { 0, 0 };
+ int mu = 0x8000000;
+ timer t;
+t :>usign1;
+ t:>seed1;
+ if(mu<0) {
+ printstr("mu must be positive");
+ return 0;
+ }
+ // init port logic
+ configure_out_port_no_ready(p_TXD, clk, 1);
+ set_clock_div(clk, CLOCKDIV);
+ start_clock(clk);
+ crc32(usign1, seed1, POLYNOMIAL);
+
+ for (int i = 0; i < ELEMENTS; i++) {
+ state[0][i] = 0;
+ state[1][i] = 0;
+ hest[i]=0;
+ crc32(usign1, seed1, POLYNOMIAL);
+ hreal[i]= (((int)usign1)>>2);
+ { hi,lo}=macs(hreal[i],hreal[i],0,0);
+ POW[0] += hi/ELEMENTS;
+ }
+ t:>usign1;
+ printstr("Error power in estimate is: ");
+ t:>seed1;
+ printuint(POW[0]);
+ t:>usign2;
+ printstr("\nSending hreal over UART");
+ txByte(p_TXD,hreal,ELEMENTS);
+ printstr("\nLMS filtering...");
+ t:>seed2;
+ crc32(usign1, seed1, POLYNOMIAL);// Create random numbers from random seed
+ crc32(usign2, seed2, POLYNOMIAL);// Create random numbers from random seed
+
+ for (int j = 0; j<LEN; j++) {
+ crc32(usign1, seed1, POLYNOMIAL);
+ x_ref=((int) usign1)>>8;
+ crc32(usign2, seed2, POLYNOMIAL);
+ noise=((int) usign2)>>16;
+ x_des=fir(x_ref, hreal, state[0], ELEMENTS);
+ errorvec[j/REDUCTION]=lms(x_ref, x_des+noise, mu, hest, state[1], ELEMENTS);
+ }
+ errorvec[LEN/REDUCTION]=mu;
+ printstr("\nError power after ");
+ printint(LEN);
+ printstr(" samples is: ");
+ for (int i = 0; i < ELEMENTS; i++) {
+ error=hreal[i]-hest[i];
+ { hi,lo}=macs(error,error,0,0);
+ POW[1] += hi/ELEMENTS;
+ }
+ printint(POW[1]);
+ printstr("\nReduction is: ");
+ if(POW[1]>0)
+ printint(POW[0]/POW[1]);
+ else
+ printstr("inf, e.g. beyond the used precision");
+ printstrln(" times");
+ printstrln("Sending hest over UART");
+ txByte(p_TXD,hest,ELEMENTS);
+ printstrln("Sending error over UART");
+ txByte(p_TXD,errorvec,1+LEN/REDUCTION);
+
+ return 0;
+}
View
4 module_lms/src/lms.h
@@ -6,6 +6,10 @@
#ifndef LMS_H_
#define LMS_H_
+
+
+extern int fir(int xn, int coeffs[], int state[], int ELEMENTS);
int lms(int x_ref, int x_des, int mu,int coeffs[], int state[],int ELEMENTS);
+void txByte(port p_TXD,int data[], unsigned size);
#endif /* LMS_H_ */
View
50 module_lms/src/lms.xc
@@ -0,0 +1,50 @@
+// Copyright (c) 2011, Mikael Bohman, All rights reserved
+// This software is freely distributable under a derivative of the
+// University of Illinois/NCSA Open Source License posted in
+// LICENSE.txt and at <http://github.xcore.com/>
+
+#include <xclib.h>
+#include <xs1.h>
+
+void txByte(port p_TXD,int data[], unsigned size) {
+ char byte;
+ int int32[1];
+ #pragma unsafe arrays
+ for (unsigned i = 0; i < size; i++) {
+ int32[0]=byterev(data[i]);
+#pragma loop unroll
+ for(unsigned j=0;j<4;j++){
+ p_TXD <: 0; //start bit
+ byte=(int32,char[])[j];
+#pragma loop unroll
+ for(int k=0;k<8;k++)
+ p_TXD <: >> byte;
+ p_TXD<: 1; //stop bit
+ }
+}}
+
+
+#pragma unsafe arrays
+int lms(int x_ref, int x_des, int mu,int coeffs[], int state[],int ELEMENTS) {
+#define shift 4;
+ unsigned ynl,pnl,muel;
+ int ynh,pnh, error,mue;
+ ynl = (1<<23);
+ ynh = 0;
+ for (int j = ELEMENTS - 1 ; j != 0 ; j--) {
+ state[j] = state[j - 1];
+ {ynh, ynl}=macs(coeffs[j], state[j], ynh, ynl);
+ }
+ state[0] = x_ref<<7;
+ {ynh, ynl}=macs(coeffs[0], x_ref<<7, ynh, ynl);
+ {pnh, pnl}=macs(state[0], state[0]/ELEMENTS, pnh, pnl);
+
+ ynh = ynh << 1 | ynl >> 31;
+
+ error = x_des-ynh;
+ {mue,muel}=macs(mu,error,0,0);
+ for(int j = ELEMENTS-1 ; j!=0 ; j--)
+ {coeffs[j],void}=macs((state[j]),mue,coeffs[j],0x80000000);
+ {coeffs[0],void}=macs((state[0]),mue,coeffs[0],0x80000000);
+ return error;
+}

0 comments on commit cf55f4f

Please sign in to comment.
Something went wrong with that request. Please try again.