Search code examples
matlabnoisenoise-reduction

How can I plot a spectrogram of wav file and plot graph before and after noise reduction input from Microphone


My project Noise reduction.

1.Here my code input from microphone and save to .wav

Read in the file

clear all;

close all;

mic1= dsp.AudioRecorder;
hmfw = dsp.AudioFileWriter('myspeech.wav','FileFormat','WAV');
disp('Speak into microphone now');
time_end = 10;
tic;
while toc <= time_end
    step(hmfw, step(mic1));
end

release(mic1);
release(hmfw);`

disp('Recording complete');
[f,fs] = audioread('C:\Users\Admin\Documents\MATLAB\myspeech.wav');`

before recorded how can i plot a spectrogram graph

time = 10 frequency = 0 - 8000

2.how to plot a spectrogram graphs after of noise reduction between frequency 500 - 2000 Hz

Like this graphs spectrogram and spectrum

Here Link

https://www.google.co.th/search?q=spectrogram+matlab&newwindow=1&rlz=1C1JPGB_enTH637TH637&espv=2&source=lnms&tbm=isch&sa=X&ei=PZliVf3GMcuIuATCroIg&ved=0CAcQ_AUoAQ&biw=1920&bih=979#imgrc=s05zemtY2IFy7M%253A%3BBgWyAcO6UJomJM%3Bhttp%253A%252F%252Fwww.aquaphoenix.com%252Flecture%252Fmatlab10%252Fimages-large%252Fmatlab_audio_funky_plot_spectrogram.jpg%3Bhttp%253A%252F%252Fwww.aquaphoenix.com%252Flecture%252Fmatlab10%252Fpage4.html%3B960%3B768

Here my filter code.

n = 7;
beginFreq = 500 / (fs/2);
endFreq = 2000 / (fs/2);
[b,a] = butter(n, [beginFreq, endFreq], 'bandpass');

thank you.


Solution

  • Try this:

    **** The previous part of your code goes here ***
    disp('Recording complete');
    [y,fs] = audioread('myspeech.wav');
    
    y = y(:,1); % Take only one channel from the recording
    
    
    n = 7;
    beginFreq = 500 / (fs/2);
    endFreq = 2000 / (fs/2);
    [b,a] = butter(n, [beginFreq, endFreq], 'bandpass');
    
    %
    dt = 1/fs;
    L = size(y,1);                    % Length of signal
    t = (0:L-1)'*dt;                  % Time vector
    
    figure(1)
    
    plot(t,y(:,1))
    title('Recording')
    xlabel('time (milliseconds)')
    
    NFFT = 2^nextpow2(L); % Next power of 2 from length of y
    Y = fft(y(:,1),NFFT)/L;
    f = fs/2*linspace(0,1,NFFT/2+1);
    
    % Plot spectrum of channel 1 from the original signal
    figure()
    plot(f,2*abs(Y(1:NFFT/2+1))); hold on
    title('Single-Sided Amplitude Spectrum of y(t)')
    xlabel('Frequency (Hz)')
    ylabel('|Y(f)|')
    
    % Filter the two channels
    filt_y = filter(b,a,y);
    
    filt_Y = fft(filt_y,NFFT)/L;
    
    % Plot the filtered spectrum of channel 1 from the original signal
    plot(f,2*abs(filt_Y(1:NFFT/2+1))) 
    title('Single-Sided Amplitude Spectrum of y(t)')
    xlabel('Frequency (Hz)')
    ylabel('|Y(f)|')