Advertisement
Guest User

MatLab noise reduction

a guest
Sep 13th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.30 KB | None | 0 0
  1. clc;
  2. clear;
  3. Fs = 8000;
  4. starting = 1;
  5. ending = 8000;
  6. load('y.mat');%contains the noisy signal
  7.  
  8. windowSize = 2048;
  9. noiseGate = zeros(windowSize, 1);
  10. sum = zeros(windowSize, 1);
  11. sumsq = zeros(windowSize, 1);
  12. profileCount = zeros(windowSize, 1);
  13. smoothing = zeros(windowSize, 1);
  14. level = 8;
  15.  
  16. portion = y(starting:ending);%gets the noise part to build a noise profile
  17.  
  18. %Noise Profile:
  19. for i=1:floor(length(portion)/windowSize)
  20.     ind = ((i-1)*windowSize)+1;
  21.     in = portion(ind:ind+windowSize-1);
  22.  
  23.     out = PowerSpectrum(windowSize, in); %FFT
  24.  
  25.     for j=1:windowSize/2
  26.         value = log(out(j));
  27.         if(value ~= inf)
  28.             sum(j) = sum(j)+value;
  29.             sumsq(j) = sumsq(j)+value^2;
  30.             profileCount(j) = profileCount(j)+1;
  31.         end
  32.     end
  33. end
  34. for i=1:windowSize/2 + 1
  35.     noiseGate(i) = sum(i)/profileCount(i);
  36. end
  37.  
  38.  
  39. %Noise Removal:
  40. for i=1:(windowSize/2):length(y)-windowSize+1
  41.     %ind = ((i-1)*windowSize)+1;
  42.     inr = y(i:i+windowSize-1);
  43.  
  44.     infft = conj(fft(inr));
  45.  
  46.     outr = real(infft);
  47.     outi = imag(infft);
  48.  
  49.     inr = WindowFunc(3, windowSize, inr);
  50.     power = PowerSpectrum(windowSize, inr);
  51.  
  52.     pLog = zeros((windowSize/2)+1, 1);
  53.     for j=1:(windowSize/2)+1
  54.         pLog(j) = log(power(j));
  55.     end
  56.  
  57.     half = windowSize/2;
  58.     for j=1:half+1;
  59.         if(pLog(j) < noiseGate(j) + (level/2))
  60.             smooth = 0.0;
  61.         else
  62.             smooth = 1.0;
  63.         end
  64.         smoothing(j) = smooth*0.5 + smoothing(j)*0.5;
  65.     end
  66.  
  67.    for j=3:half-1
  68.       if (smoothing(j)>=0.5 && smoothing(j)<=0.55 && smoothing(j-1)<0.1 && smoothing(j-2)<0.1 && smoothing(j+1)<0.1 && smoothing(j+2)<0.1)
  69.           smoothing(j) = 0.0;
  70.       end
  71.    end
  72.  
  73.    outr(1) = outr(1)*smoothing(1);
  74.    outi(1) = outi(1)*smoothing(1);
  75.    outr(half+1) = outr(half+1)*smoothing(half+1);
  76.    outi(half+1) = outi(half+1)*smoothing(half+1);
  77.  
  78.    for j=2:half
  79.        k = windowSize - (j-2);
  80.        smooth = smoothing(j);
  81.        outr(j) = outr(j)*smooth;
  82.        outi(j) = outi(j)*smooth;
  83.        outr(k) = outr(k)*smooth;
  84.        outi(k) = outi(k)*smooth;    
  85.    end
  86.  
  87.    outTmp = outr - 1j.*outi;
  88.    inr = real(ifft(conj(outTmp)));
  89.    inr = WindowFunc(3, windowSize, inr);
  90.  
  91.    yFF(i:i+(windowSize/2)-1) = inr(1:windowSize/2);
  92.  
  93. end
  94.  
  95.  sound(yFF, 8000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement