// In file F:\matlab\ClassLoad.java // Copyright 2004 Paul Milenkovic, 118 Shiloh Dr., Madison, WI 53705 U.S.A. // Free software: Permission granted to use, copy, excerpt or share this // source code and derived executables provided that this notice is maintained. // ClassLoad: load/reload class loader for the Matlab environment. // Example -- // F:\matlab working directory, contains a public interface (not reloadable) // F:\matlab\myjava is subdirectory for reloadable Java class implementing // that interface (required). // // From Matlab command line // >>cl = ClassLoad('F:\\matlab\\myjava/') // designates a directory F:\matlab\myjava to contain your reloadable class or // classes (must be other than F:\matlab or reloading will not work). Invoking // >>myclass = cl.NewInstance('MyClass') // creates a Matlab-accessible object. After changing and recompiling MyClass, // >>myclass = cl.NewInstance('MyClass') // will create a new object myclass that has the changes. import java.net.*; import java.io.*; import java.lang.reflect.*; public class ClassLoad { // Handler class required by Proxy -- all this handler does is forward all // method calls using the reflection API invoke() method. class ProxyHandler implements InvocationHandler { private Object proxy_target; public ProxyHandler(Object target) {proxy_target = target;} public Object invoke(Object proxy, Method pmethod, Object[] params) throws Exception { return pmethod.invoke(proxy_target,params); } } private URL[] urlL; // off-classpath search path list for reloadable class public ClassLoad(String pname) throws Exception { urlL = new URL[] {(new File(pname)).toURL()}; } public Object NewInstance(String cname) throws Exception { ClassLoader ccl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = new URLClassLoader(urlL); Class c = Class.forName(cname,true,cl); Object o = c.newInstance(); // Return proxy object because Matlab is otherwise unable to see // object in new class loader namespace. return Proxy.newProxyInstance(ccl,c.getInterfaces(),new ProxyHandler(o)); } } // In file F:\matlab\InPlace.java // Copyright 2004 Paul Milenkovic, 118 Shiloh Dr., Madison, WI 53705 U.S.A. // Free software: Permission granted to use, copy, excerpt or share this // source code and derived executables provided that this notice is maintained. public interface InPlace { public void SetParams(double[] params); public void Init(double sample_ms); public float[] Apply(float[] sig); } // In file F:\matlab\myjava\SineFilter.java // Copyright 2004 Paul Milenkovic, 118 Shiloh Dr., Madison, WI 53705 U.S.A. // Free software: Permission granted to use, copy, excerpt or share this // source code and derived executables provided that this notice is maintained. public class SineFilter implements InPlace { // filter parameters private double freq_Hz = 100.0; private double magnitude = 1.0; private double phase = 0.0; // filter coefficients w, internal state y private double wr, wi; private double yr, yi; public void SetParams(double[] params) { if (params.length > 0) freq_Hz = params[0]; if (params.length > 1) magnitude = params[1]; if (params.length > 2) phase = params[2]; } public void Init(double sample_ms) { // Calculate coefficients from filter parameters double Pi = 3.141592654; double iphase = 2*Pi*phase/360; double dphase = 2*Pi*freq_Hz*0.001*sample_ms; wr = Math.cos(dphase); wi = Math.sin(dphase); // Initialize state yr = magnitude*Math.cos(iphase); yi = magnitude*Math.sin(iphase); } public float[] Apply(float[] sig) { for (int i = 0; i < sig.length; i++) { double temp = yr*wr - yi*wi; sig[i] += (float) yr; yi = yr*wi + yi*wr; yr = temp; } return sig; } }