Thursday, 4 June 2015

Adaptive filters



Objectives:

·        To know how to implement a low pass filter, high pass filter, band pass filter and band stop filter using IIR filter.
·        To learn how to implement an inverse filter for low pass filter and band pass filter.

Equipment and tools:

·        A computer having code composal studio and Matlab.
·        A 6713dsk toolkit.
·        A picoscope.
Theory
Adaptive filters are filters with varying coefficients. These filters are used in applications where a given system is changing its coefficients in an unknown manner. A typical system can be either a communication channel whose coefficients are changing with either temperature or number of users.

In such cases it is highlydesirable to design the filter to be self-learning so that it can adapt itself to the situationat hand. The coefficients of an adaptive filter are adjusted to compensate for changes ininput signal, output signal, or system parameters. Instead of being rigid, an adaptivesystem can learn the signal characteristics and track slow changes. An adaptive filtercan be very useful when there is uncertainty about the characteristics of a signal orwhen these characteristics change.
 
Conceptually, the adaptive scheme is fairly simple. Most of the adaptive scheme scan be described by the structure shown in Figure 1. This is a basic adaptive filter structure in which the adaptive filter’s output  is compared with a desired signal to yield an error signal , which is fed back to the adaptive filter.

The error signal is input to the adaptive algorithm, which adjusts the filter’s coefficients to satisfy some predetermined criteria or rules. The desired signal is usually the most difficult one to obtain. One of the first questions that probably comes to mind is:Why are we trying to generate the desired signal at  if we already know it? Surprisingly, in many applications the desired signal does exist somewhere in the system or is known a prior.The challenge in applying adaptive techniques is to figure out where to get the desired signal, what to make the output , and what to make the error .
 
Figure 1Basic adaptive filter structure.
The coefficients of the adaptive filter are adjusted, or optimized, using an LMS algorithm based on the error signal. Here we discuss only the LMS searching algorithm with a linear combiner (FIR filter), although there are several strategies for performing adaptive filtering. The output of the adaptive filter in Figure 1 is
 

Applications of adaptive filters
Adaptive filters have been used for different applications such as:
1.     For noise cancellation as illustrated inFigure 2. The desired signal  is corrupted by uncorrelated additive noise . The input to the adaptive filter is a noise that is correlated with the noise . The noise  could come from the same source as  but modified by the environment. The adaptive filter’s output  is adapted to the noise . When this happens, the error signal approaches the desired signal . The overall output is this error signal and not the adaptive filter’s output . If  is uncorrelated with , the strategy is to minimize , where  is the expected value. The expected value is generally unknown;therefore, it is usually approximated with a running average or with the instantaneousfunction itself. Its signal component,, will be unaffected and onlyits noise component  will be minimized.
 
Figure 2Adaptive filter structure for noise cancellation.
Experimental procedures
In this experiment an adaptive filter for noise cancellation and system identification will be designed and implemented.
Adaptive Filter for Sinusoidal Noise Cancellation
This example illustrates the application of the LMS criterion to cancel an undesirable sinusoidal noise.
A desired sine wave of 1500 Hz with an additive (undesired) sine wave noise of 312 Hz forms one of two inputs to the adaptive filter structure. A reference (template) cosine signal, with a frequency of 312Hz, is the input to a 30-coefficient adaptive FIR filter. The 312-Hz reference cosine signal is correlated with the 312-Hz additive sine noise but not with the 1500-Hz desired sine signal.
For each time , the output of the adaptive FIR filter is calculated and the 30 weights or coefficients are updated along with the delay samples. The error signal  is the overall desired output of the adaptive structure. This error signal is the difference between the desired signal and additive noise (dplusn) and the adaptive filter’s output, .
To perform these parts of the experiment follow these steps
1.     In MATLAB,generate the desired signal, the noise plus the desired signal and the reference noise according to the following MATLAB commands
 

%Adaptnoise.m Generates: dplusn.h, refnoise.h, and sin1500.h
for i=1:128
desired(i) = round(100*sin(2*pi*(i-1)*1500/8000)); %sin(1500)
addnoise(i) = round(100*sin(2*pi*(i-1)*312/8000)); %sin(312)
refnoise(i) = round(100*cos(2*pi*(i-1)*312/8000)); %cos(312)
end

dplusn=awgn(desired,1);
refnoise=(noisy_sig-sin1500);

fid=fopen('sin1500.h','w'); %desired sin(1500)
fprintf(fid,'short sin1500[128]={');
fprintf(fid,'%d, ' ,desired(1:127));
fprintf(fid,'%d' ,desired(128));
fprintf(fid,'};\n');
fclose(fid);

fid=fopen('dplusn.h','w'); %desired + noise
fprintf(fid,'shortdplusn[128]={');
fprintf(fid,'%d, ' ,dplusn(1:127));
fprintf(fid,'%d' ,dplusn(128));
fprintf(fid,'};\n');
fclose(fid);

fid=fopen('refnoise.h','w'); %reference noise
fprintf(fid,'shortrefnoise[128]={');
fprintf(fid,'%d, ' ,refnoise(1:127));
fprintf(fid,'%d' ,refnoise(128));
fprintf(fid,'};\n');
fclose(fid);

1.     In your code composer studio use the following code to implement an adaptive filter
#include "DSK6713_AIC23.h" //codec-DSK support file
Uint32 fs= DSK6713_AIC23_FREQ_8KHZ; //set sampling rate
#include "refnoise.h" //cosine 312 Hz
#include "dplusn.h" //sin(1500) + sin(312)
#define beta 1E-10 //rate of convergence
#define N 30 //# of weights (coefficients)
#define NS 128 //# of output sample points
float w[N]; //buffer weights of adapt filter
float delay[N]; //input buffer to adapt filter
short output; //overall output
short out_type = 1; //output type for slider
interrupt void c_int11() //ISR
{
short i;
static short buffercount=0; //init count of # out samples
float yn, E; //output filter/"error" signal
delay[0] = refnoise[buffercount]; //cos(312Hz) input to adapt FIR
yn = 0; //init output of adapt filter
for (i = 0; i < N; i++) //to calculate out of adapt FIR
yn += (w[i] * delay[i]); //output of adaptive filter
E = dplusn[buffercount] - yn; //"error" signal=(d+n)-yn
for (i = N-1; i >= 0; i--) //to update weights and delays
{
w[i] = w[i] + beta*E*delay[i]; //update weights
delay[i] = delay[i-1]; //update delay samples
}
buffercount++; //increment buffer count
if (buffercount>= NS) //if buffercount=# out samples
buffercount = 0; //reinit count
if (out_type == 1) //if slider in position 1
output = ((short)E*10); //"error" signal overall output
else if (out_type == 2) //if slider in position 2
output=dplusn[buffercount]*10; //desired(1500)+noise(312)
output_sample(output); //overall output result
return; //return from ISR
}
void main()
{
short T=0;
for (T = 0; T < 30; T++)
{
w[T] = 0; //init buffer for weights
delay[T] = 0; //init buffer for delay samples
}
comm_intr(); //init DSK, codec, McBSP
while(1); //infinite loop
}
2.     Write a slider code to change the parameter out_type from 1 to 2
3.     Build and execute the project
4.     Plot the signal that as you see on the screen of the oscilloscope if the out_type variable is set to 1.

 







Question 1.              What signal you are measuring on the oscilloscope screen?
The signal is that signal we desire pluse the noise.
1.     Plot the signal that as you see on the screen of the oscilloscope if the out_type variable is set to 2.
 
Question 1. What signal you are measuring on the oscilloscope screen?It is the sinal we desire to have after it goes throw the adabtve filter and the noise canceld due to noise cancelation.
1.     Repeat the experiment by using Gaussian error signal rather than using a sinusoidal signal
Adaptive FIR filter for noise cancellation using external inputs
    This example extends the previous one to cancel an undesirable sinusoidal noise using external inputs. The source program shown below allows two external inputs: a desired signal and a sinusoidal interference. The program uses the union structure introduced in Chapter 2 with the project example loop_stereo. A 32-bit signal is captured using this structure that allows an external 16-bit input signal through each channel. The 16-bit desired signal is input through the left channel and the undesirable 16-bit signal through the right channel. An adapter with two connectors at one end for each input signal and one connector at the other end, which connects to the DSK, was introduced in Chapter 2 with the loop_stereo project and is required to implement this example. The basic adaptive structure in Figure 2 is applied here along with the LMS algorithm.

#include "DSK6713_AIC23.h" //codec-DSK support file
Uint32 fs=DSK6713_AIC23_FREQ_48KHZ; //set sampling rate
#define beta 1E-13 //rate of convergence
#define N 30 //# of weights (coefficients)
#define LEFT 0                                                                                 //left channel
#define RIGHT 1 //right channel
float w[N]; //weights for adapt filter
float delay[N];//input buffer to adapt filter
short output;//overall output
short out_type = 1;//output type for slider
volatile union{unsigned intuint; short channel[2];}AIC23_data;
interrupt void c_int11()//ISR
{
short i;
float yn=0, E=0, dplusn=0, desired=0, noise=0;
AIC23_data.uint = input_sample();//input 32-bit from both channels
dplusn =(AIC23_data.channel[LEFT]);//input left channel
noise = (AIC23_data.channel[RIGHT]);//input right channel
delay[0] = noise; //noise as input to adapt FIR
for (i = 0; i < N; i++) //to calculate out of adapt FIR
yn += (w[i] * delay[i]); //output of adaptive filter
E = (dplusn) - yn; //"error" signal=(d+n)-yn
for (i = N-1; i >= 0; i--) //to update weights and delays
{
w[i] = w[i] + beta*E*delay[i]; //update weights
delay[i] = delay[i-1]; //update delay samples
}
if(out_type == 1) //if slider in position 1
output=((short)E); //error signal as overall output
else if(out_type==2) //if slider in position 2
output=((short)dplusn); //output (desired+noise)
output_sample(output); //overall output result
return;
}
void main()
{
short T=0;
for (T = 0; T < 30; T++)
{
w[T] = 0; //init buffer for weights
delay[T] = 0; //init buffer for delay samples
}
comm_intr(); //init DSK, codec, McBSP
while(1); //infinite loop
}

Using your PC soundcard and the MATLAB. Generate 1.5 kHz sinusoidal signal from the function generator to represent the desired signal and another sinusoidal signal with 2 kHz; which represents the additive noise signal; and another cosine 2 kHz signal to represent reference noise as shown in Figure 6
 
1.     Run the program. Verify that the 2-kHz noise signal is being canceled gradually. You can adjust the rate of convergence by changing beta by a factor of 10 in the program.
2.     Access/load the slider program adaptnoise_2IN.gel and change the slider position from 1 to 2. Verify the output as the two original sinusoidal signals at 1.5 kHz and at 2 kHz.
3.     Desired: wideband random noise; undesired: 2 kHz. Input random noise (from a noise generator, MATLAB.) as the desired wideband signal into the left input channel and the undesired 2-kHz sinusoidal noise signal into the right input channel. Restart/run the program. Verify that the 2-kHz sinusoidal noise signal is being canceled gradually, with the wideband random noise signal remaining. With the slider in position 2, observe that both the undesired and desired input signals are.

Conclusion:


·        The adaptive filters can be used to implement a LPF, HPF, BBF and BSF.

·        These filters are used to process the signals according to requirements.

·        In thaadaptive filters we can use it to belt noise cancelation to the singnal .

·        We can use the filter to filter the voice  and make difrent cut off frequances.  


No comments: