Airplane Noise Removal Demonstration: Difference between revisions

From Class Wiki
Jump to navigation Jump to search
Created page with ' % Demonstration of LMS algorithm for noise cancellation. % Rich Kozick, Spring 1997 % Rob Frohne's modifications for Aircraft Noise Cancellation. % modifications for Macintos…'
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
  % Demonstration of LMS algorithm for noise cancellation.
  <nowiki>
% Rich Kozick, Spring 1997
% Demonstration of LMS algorithm for noise cancellation.
% Rob Frohne's modifications for Aircraft Noise Cancellation.
% Rich Kozick, Spring 1997
% modifications for Macintosh 2000. (commented out here)
% Rob Frohne's modifications for Aircraft Noise Cancellation.
% Rob Frohne's modifications for Linux.  
% modifications for Macintosh 2000. (commented out here)
% Rob Frohne's modifications for Linux.
% Aircraft noise cancellation simulation.
% Desired signal
clear all
Totaltime=3;
system("espeak 'Hit a key and speak the signal.'");
Fs=8000; %sample rate.


% Aircraft noise cancellation simulation.
st=record(Totaltime,Fs);


  % Desired signal
s = st';
Ls = length(s);
% Interference + random noise
system("espeak 'Hit a key and make the noise!'");
%pause();
nt = record(Totaltime, Fs);
n = nt';
%Sign = 0.01;
%Dn=20; % Delay of the noise that appears in y.
%n = n(1:Ls) + Sign*randn(Ls,1);
%an = [0 .01 -.5 1 -.5 .1 .01 0];
%bn = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
bn=rand(1,20);
an = [1];
%sys = tf(an,bn,1/Fs);
%bode(sys);
%figure(1);
nf = filter(bn,an,n);
%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);
y = s + nf;
%Sigx = 0.01; 
%ax = [1];      % bx and ax are filtering on n to produce x
%bx = [0 0 0 .1 1 .1];
%Dx = 1;        % Delay of n that appears in x
%x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);
%x = x + Sigx*randn(1,Ls);


clear all
system("espeak 'Here is the noisy signal.'");
Totaltime=3;
soundsc(y,Fs);
system("aoss espeak 'Hit a key and speak the signal.'");


Fs=8000; %sample rate.
N = 20;         % Length of adaptive filter
st=record(Totaltime,Fs);
s = st';
Ls = length(s);


% Interference + random noise
% LMS algorithm for adaptive noise cancellation
system("aoss espeak 'Hit a key and make the noise!'");
%pause();
nt = record(Totaltime, Fs);
n = nt';
%Sign = 0.01;
%Dn=20;  % Delay of the noise that appears in y.
%n = n(1:Ls) + Sign*randn(Ls,1);
%an = [0 .01 -.5 1 -.5 .1 .01 0];
%bn = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
bn=rand(1,20);
an = [1];
%sys = tf(an,bn,1/Fs);
%bode(sys);
%figure(1);
nf = filter(bn,an,n);
%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);


h = zeros(N,1);
%mu = 1/(10*N*var(n));
mu = .05;
for k=N:Ls
  xk = n(k:-1:(k-N+1));
  nhat(k) = h'*xk';
  e(k) = - y(k) + nhat(k);
  h = h - mu*e(k)*xk'/(xk*xk'); % For the previous line.
end


  y = s + nf;
% The signal estimate is in the vector e
%speak('Here is the cleaned signal.');
system("espeak 'Here is the cleaned signal.'");
soundsc(e(1,6000:end),Fs);
%playaudio(e); (For Linux without octave-forge.)
</nowiki>
The record.m function in Ubuntu Maverick is broken.  You can use this one instead.


%Sigx = 0.01; 
%ax = [1];      % bx and ax are filtering on n to produce x
%bx = [0 0 0 .1 1 .1];
%Dx = 1;        % Delay of n that appears in x
%x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);
%x = x + Sigx*randn(1,Ls);


  system("aoss espeak 'Here is the noisy signal.'");
  ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003, 2004, 2005,
  soundsc(y,Fs);
##              2006, 2007 John W. Eaton
 
##
  N = 20;         % Length of adaptive filter
## This file is part of Octave.
 
##
  % LMS algorithm for adaptive noise cancellation
## Octave is free software; you can redistribute it and/or modify it
 
## under the terms of the GNU General Public License as published by
  h = zeros(N,1);
  ## the Free Software Foundation; either version 3 of the License, or (at
  %mu = 1/(10*N*var(n));
## your option) any later version.
  mu = .05;
##
  for k=N:Ls
## Octave is distributed in the hope that it will be useful, but
    xk = n(k:-1:(k-N+1));
  ## WITHOUT ANY WARRANTY; without even the implied warranty of
    nhat(k) = h'*xk';
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    e(k) = - y(k) + nhat(k);
  ## General Public License for more details.
    h = h - mu*e(k)*xk'/(xk*xk'); % For the previous line.
##
  end
  ## You should have received a copy of the GNU General Public License
 
## along with Octave; see the file COPYING.  If not, see
% The signal estimate is in the vector e
  ## <http://www.gnu.org/licenses/>. 
%speak('Here is the cleaned signal.');
  system("aoss espeak 'Here is the cleaned signal.'");
## -*- texinfo -*-
  soundsc(e(1,6000:end),Fs);
## @deftypefn {Function File} {} record (@var{sec}, @var{sampling_rate})
  %playaudio(e);  (For Linux without octave-forge.)
  ## Records @var{sec} seconds of audio input into the vector @var{x}. The
  ## default value for @var{sampling_rate} is 8000 samples per second, or
## 8kHz.  The program waits until the user types @key{RET} and then
## immediately starts to record.
## @seealso{lin2mu, mu2lin, loadaudio, saveaudio, playaudio, setaudio}
## @end deftypefn
## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
## Created: 19 September 1994
## Adapted-By: jwe
## And adapted again 11/25/2010 by Rob Frohne
function X = record (sec, sampling_rate)
  if (nargin == 1)
    sampling_rate = 8000;
  elseif (nargin != 2)
    print_usage ();
  endif
  file = tmpnam ();
  file= [file,".wav"];
   
  input ("Please hit ENTER and speak afterwards!\n", 1);
   
  cmd = sprintf ("rec -c1 -r%d %s trim 0 %d",
                    sampling_rate, file, sec)
   
  system (cmd);
   
  X = wavread(file);
   
endfunction

Latest revision as of 12:27, 7 December 2015

% Demonstration of LMS algorithm for noise cancellation.
% Rich Kozick, Spring 1997
% Rob Frohne's modifications for Aircraft Noise Cancellation.
% modifications for Macintosh 2000. (commented out here)
% Rob Frohne's modifications for Linux. 
% Aircraft noise cancellation simulation.
% Desired signal
clear all
Totaltime=3;
system("espeak 'Hit a key and speak the signal.'");
Fs=8000; %sample rate.

st=record(Totaltime,Fs);

s = st';
Ls = length(s);
% Interference + random noise
system("espeak 'Hit a key and make the noise!'");
%pause();
nt = record(Totaltime, Fs);
n = nt';
%Sign = 0.01;
%Dn=20;  % Delay of the noise that appears in y.
%n = n(1:Ls) + Sign*randn(Ls,1);
%an = [0 .01 -.5 1 -.5 .1 .01 0];
%bn = 4*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5];
bn=rand(1,20);
an = [1];
%sys = tf(an,bn,1/Fs);
%bode(sys);
%figure(1);
nf = filter(bn,an,n);
%nf = [nf(Dn:(Ls)); zeros(Dn-1,1)] + Sign*randn(Ls,1);
y = s + nf;
%Sigx = 0.01;  
%ax = [1];       % bx and ax are filtering on n to produce x
%bx = [0 0 0 .1 1 .1];
%Dx = 1;        % Delay of n that appears in x
%x = filter(bx, ax, n);%x = [x(Dx:(Ls)); zeros(Dx-1,1)] + Sigx*randn(Ls,1);
%x = x + Sigx*randn(1,Ls);

system("espeak 'Here is the noisy signal.'");
soundsc(y,Fs);

N = 20;         % Length of adaptive filter

% LMS algorithm for adaptive noise cancellation

h = zeros(N,1);
%mu = 1/(10*N*var(n));
mu = .05;
for k=N:Ls
   xk = n(k:-1:(k-N+1));
   nhat(k) = h'*xk';
   e(k) = - y(k) + nhat(k);
   h = h - mu*e(k)*xk'/(xk*xk'); % For the previous line.
end

% The signal estimate is in the vector e
%speak('Here is the cleaned signal.');
system("espeak 'Here is the cleaned signal.'");
soundsc(e(1,6000:end),Fs);
%playaudio(e);  (For Linux without octave-forge.)

The record.m function in Ubuntu Maverick is broken. You can use this one instead.


## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003, 2004, 2005,
##               2006, 2007 John W. Eaton
##
## This file is part of Octave.
##
## Octave is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.   

## -*- texinfo -*-
## @deftypefn {Function File} {} record (@var{sec}, @var{sampling_rate})
## Records @var{sec} seconds of audio input into the vector @var{x}.  The
## default value for @var{sampling_rate} is 8000 samples per second, or
## 8kHz.  The program waits until the user types @key{RET} and then
## immediately starts to record.
## @seealso{lin2mu, mu2lin, loadaudio, saveaudio, playaudio, setaudio}
## @end deftypefn

## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
## Created: 19 September 1994
## Adapted-By: jwe
## And adapted again 11/25/2010 by Rob Frohne

function X = record (sec, sampling_rate)

  if (nargin == 1)
    sampling_rate = 8000;
  elseif (nargin != 2)
    print_usage ();
  endif

  file = tmpnam ();
  file= [file,".wav"];

  input ("Please hit ENTER and speak afterwards!\n", 1);

  cmd = sprintf ("rec -c1 -r%d %s trim 0 %d",
                   sampling_rate, file, sec)

  system (cmd);

  X = wavread(file);

endfunction