Matlab Tutorial : Manipulating Audio I
In chapter Vectors (arrays) with Audio Files, we played with audio files. In this chapter, we'll see we can do a lot more with audio.
First, we need to read in the audio files using wavread() into arrays:
>> [beautiful, fs] = wavread('C:\SOUND\BeautifulLife.wav'); >> rev_b = flipud(beautiful); >> sound(rev_b, fs); >> soundsc(rev_b, fs);
The files is available: BeautifulLife.wav
The following script draws two pictures: normal signal and reversed signal.
[beautiful, fs] = wavread('C:\SOUND\BeautifulLife.wav'); b = beautiful(1: fs*40); rev_b = flipud(b); time = (1/fs)*length(b); t = linspace(0, time, length(b)); subplot(211); plot(t,b); xlabel('time(sec)'); ylabel('signal strength'); title('Beautiful Life'); subplot(212); plot(t,rev_b); xlabel('time(sec)'); ylabel('signal strength'); title('Reversed Beautiful Life');
- sound(y,Fs): sends the signal in vector y (with sample frequency Fs) to the speaker on the PC and most UNIX platforms. Values in y are assumed to be in the range -1.0 y 1.0. Values outside that range are clipped. Stereo sound is played on platforms that support it when y is an n-by-2 matrix.
- soundsc(y,Fs): sends the signal in vector y (with sample frequency Fs) to the speaker on the PC and most UNIX platforms. The signal y is scaled to the range -1.0 y 1.0 before it is played, resulting in a sound that is played as loud as possible without clipping.
In this section, we'll make an echo sound by adding delayed signals to the original input. The following code does it:
% [beautiful, fs] = wavread('C:\SOUND\BeautifulLife.wav'); [beautiful, fs] = audioread('C:\SOUND\BeautifulLife.wav'); nSec = 90; b = beautiful(1: fs*nSec); b_echo = b; N = fs/2; for n = N+1 : length(b) % adding N off the phase sound to the original input. b_echo(n) = b(n) + b(n-N); end time = (1/fs)*length(b); t = linspace(0, time, length(b)); plot(t,b,'k',t,b_echo,'r'); % hard to tell because too many data xlabel('time(sec)'); ylabel('signal strength'); title('Beautiful Life');
The code first sets the output to be the input:
b_echo = b;
This is simply a quick way to initialize the output array to the proper size (makes it operate faster).
The loop starts at n=fs/2+1 and goes up to the full length of our array b. The output is the sum of the input at sample time n plus the input at sample time n-N (11025 samples ago, 11025/22050 seconds ago since the samples are spaced by 1/22050 seconds).
b_echo(n) = b(n) + b(n-N);
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization