Advertisement
TheVideoVolcano

High Pass Filter

Apr 2nd, 2020
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.09 KB | None | 0 0
  1. %codesrc: https://stackoverflow.com/questions/24195089/remove-noise-from-wav-file-matlab
  2.  
  3. clearvars; %clear existing variables
  4. close all; %close any open dialogue
  5.  
  6. fName= 'sentence_with_80hz.wav'; %specify the source file in the working dir
  7. cd 'C:\Users\Ric\Desktop\UNI\Signal II\Assignment' %Navigate to folder containing to audio file
  8. [f,fs] = audioread(fName); %read and sample audio file data
  9.  
  10. plot(f, 'k');
  11. title('Original Waveform');
  12. xlabel('Time');
  13. ylabel('Amplitude');
  14.  
  15. %pOrig = audioplayer(f, fs);
  16. %pOrig.play;
  17.  
  18. %-----Plot the discrete data points of the waveform------
  19.  
  20. N=size(f,1); %Get the total number of samples
  21. figure; %create figure window
  22. subplot(2,1,1);
  23. stem(1:N, f(:,1),'k'); %plot discrete datapoints
  24. title('Left Channel');
  25. xlabel('Time');
  26. ylabel('Amplitude');
  27. subplot(2,1,2);
  28. stem(1:N, f(:,2),'k'); %plot discrete datapoints
  29. title('Right Channel');
  30. xlabel('Time');
  31. ylabel('Amplitude');
  32. %-------------------------------------------------
  33.  
  34. %-----Plot the spectrum using Nyquist magic------
  35. df =fs/N;
  36. w=(-(N/2):(N/2)-1)*df;
  37. y=fft(f(:,1), N) / N; %normalising
  38. y2 =fftshift(y); %centre at 0Hz
  39. figure %create figure window
  40. plot(w,abs(y2),'k');
  41. title('Frequency Spectrum');
  42. xlabel('Frequency (Hz)');
  43. ylabel('Amplitude');
  44. %-------------------------------------------------
  45.  
  46. %--------Filtering--------------------------------
  47.  
  48. n=7; %order 7 performs best
  49. cutOff = 160 / (fs/2);
  50.  
  51. %80Hz Noise signal appears to requires twice the freq thanks to the transision band!
  52. [b,a] = butter(n, cutOff, 'high'); %high pass filter to eliminate 80Hz noise signal
  53. freqz(b,a);
  54. fvtool(b,a,'analysis', 'freq');
  55. fOut = filter(b, a, f); %filter the audio using the butter filter coeffients (coef1, coef2, audiodata)
  56.  
  57. %--------------------------------------------------
  58.  
  59. %-------------------output-------------------------
  60.  
  61. plot(fOut, 'k'); %black signal
  62. title('Filtered Waveform');
  63. xlabel('Time');
  64. ylabel('Amplitude');
  65.  
  66. p = audioplayer(fOut, fs);
  67. p.play;
  68. audiowrite('test.wav', fOut, fs);
  69. %write processes audio to wavfile
  70.  
  71. %--------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement