Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %codesrc: https://stackoverflow.com/questions/24195089/remove-noise-from-wav-file-matlab
- clearvars; %clear existing variables
- close all; %close any open dialogue
- fName= 'sentence_with_80hz.wav'; %specify the source file in the working dir
- cd 'C:\Users\Ric\Desktop\UNI\Signal II\Assignment' %Navigate to folder containing to audio file
- [f,fs] = audioread(fName); %read and sample audio file data
- plot(f, 'k');
- title('Original Waveform');
- xlabel('Time');
- ylabel('Amplitude');
- %pOrig = audioplayer(f, fs);
- %pOrig.play;
- %-----Plot the discrete data points of the waveform------
- N=size(f,1); %Get the total number of samples
- figure; %create figure window
- subplot(2,1,1);
- stem(1:N, f(:,1),'k'); %plot discrete datapoints
- title('Left Channel');
- xlabel('Time');
- ylabel('Amplitude');
- subplot(2,1,2);
- stem(1:N, f(:,2),'k'); %plot discrete datapoints
- title('Right Channel');
- xlabel('Time');
- ylabel('Amplitude');
- %-------------------------------------------------
- %-----Plot the spectrum using Nyquist magic------
- df =fs/N;
- w=(-(N/2):(N/2)-1)*df;
- y=fft(f(:,1), N) / N; %normalising
- y2 =fftshift(y); %centre at 0Hz
- figure %create figure window
- plot(w,abs(y2),'k');
- title('Frequency Spectrum');
- xlabel('Frequency (Hz)');
- ylabel('Amplitude');
- %-------------------------------------------------
- %--------Filtering--------------------------------
- n=7; %order 7 performs best
- cutOff = 160 / (fs/2);
- %80Hz Noise signal appears to requires twice the freq thanks to the transision band!
- [b,a] = butter(n, cutOff, 'high'); %high pass filter to eliminate 80Hz noise signal
- freqz(b,a);
- fvtool(b,a,'analysis', 'freq');
- fOut = filter(b, a, f); %filter the audio using the butter filter coeffients (coef1, coef2, audiodata)
- %--------------------------------------------------
- %-------------------output-------------------------
- plot(fOut, 'k'); %black signal
- title('Filtered Waveform');
- xlabel('Time');
- ylabel('Amplitude');
- p = audioplayer(fOut, fs);
- p.play;
- audiowrite('test.wav', fOut, fs);
- %write processes audio to wavfile
- %--------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement