Last active
July 11, 2017 09:06
-
-
Save JarvusChen/978601f6b80f183d5eb2f541c25d174f to your computer and use it in GitHub Desktop.
MATLAB common code in Audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% Updated on Jun., 2017 ------------------------------------------------ | |
% Copyright by Jarvus Chen. All Rights Reserved. | |
% www.jarvus.net | |
%% Read and Plot --------------------------------------------------------- | |
clear | |
clc | |
close all | |
% read audio file | |
[ clean, fs ] = wavread( 'speech.wav' ); | |
s_length = length(clean); % get the amount of all samples | |
sampleTime = ( 1:s_length )/fs;% transfer sample amount to its time | |
frameSize = fix(0.020*fs); % window length is 20 ms | |
NFFT = 2*frameSize; % FFT size is twice the window length | |
figure; | |
% plot audio in time domain | |
plot(sampleTime, clean); | |
title(['Signal - clean speech']) | |
xlabel('Time (s)');ylabel('Amplitude'); | |
figure; | |
% plot audio in frequency domain | |
% use hanning window and 50% overlap | |
[B,f,T] = specgram(clean,NFFT,fs,hanning(frameSize),frameSize/2); | |
imagesc(T,f,20*log10(abs(B)));axis xy;colorbar | |
title(['Spectrogram - clean speech']) | |
xlabel('Time (s)');ylabel('Frequency (Hz)'); | |
%% Scale and Write --------------------------------------------------------- | |
% scale your max amplitude to (0~1) | |
ampScale = 0.7; | |
out = zeros(s_length,1); | |
if max(clean) > abs(min(clean)) | |
out = clean*(ampScale/max(clean)); | |
else | |
out = clean*((-ampScale)/min(clean)); | |
end | |
% write new audio file | |
wavwrite(out,fs,16,'output.wav'); | |
%% change the sample rate of audio --------------------------------------------------------- | |
fs_from = 44100; | |
fs_to = 16000; | |
[P,Q] = rat(fs_to/fs_from); | |
clean16 = resample(clean, P, Q); | |
%% align two signal which are with time delay --------------------------------------------------------- | |
% masked is another signal which is time delay | |
% or a little different by clean | |
% clean_m and masked_m is modified signal which are aligned | |
[clean_out ,masked_out] = alignsignals(clean, masked); | |
[pos] = find(masked_out ~= 0); | |
clean_out = clean_out(pos(1):length(clean)); | |
masked_out = masked_out(pos(1):length(clean)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment