Matlab Tutorial : Manipulating Audio II
In chapter Vectors (arrays) with Audio Files. Also, in chapter Manipulating audio I, we reversed audio files and made echo.
In this chapter, we'll see there are still a lot more things we can do with audio.
First, we need to read in the audio files using wavread() into arrays:
>> [sis, fs] = wavread('Alone-Sistar.wav'); >> sound(sis, fs);
The files is available: Alone-Sistar.wav
Original file was Alone-Sistar.mp3 is cut using ffmpeg:
$ ffmpeg -ss 00:00:00.00 -t 30 -i Alone-Sistar.mp3 -ab 256k -y Alone-Sistar.wav
in chapter Manipulating audio II, we made an echo with the following script:
for n = N+1 : length(s) % adding N off the phase sound to the original input. s_echo(n) = s(n) + 1.0*s(n-N); end
Now, we want to add softer echo sound to the original input:
[sis, fs] = wavread('Alone-Sistar.wav'); nSec = 30 s = sis(1: fs*nSec); s_echo = s; N = fs/2; for n = N+1 : length(s) % adding N off the phase with 0.5 signal strength % sound to the original input. s_echo(n) = s(n) + 0.5*s(n-N); end % audio save wavwrite(s_echo, fs, 'sis_echo_50.wav') % audio play sound(s,fs); % plot time = (1/fs)*length(s); t = linspace(0, time, length(s)); plot(t,s_echo,'b'); xlabel('time(sec)'); ylabel('signal strength'); title('Sistar - Alone with 50% echo');
The output sound: sis_echo_50.wav
We can compare it with:
- Original input: Alone-Sistar.wav
- Echo with full strength : sis_echo_full.wav
Here are the three plots: Original, Full strength echo, and 50% strength echo:
Well, hard to tell the differences with the pictures. However, note the scales of the plots have changed because the last two were super-imposed ones with different echoes.
Now, let's play with stereo sound. If we check the size of the variable sis:
>> size(sis) ans = 1322471 2
We can see the sound is stereo, the 2 tells it. Or if we check the array, it shows the strength of the right and left audios:
>> sis(100000:100010,:) ans = -0.0449 -0.0255 -0.0211 -0.0107 0.0013 -0.0006 0.0208 -0.0019 0.0397 -0.0099 0.0602 -0.0152 0.0802 -0.0216 0.0972 -0.0316 0.1125 -0.0316 0.1273 -0.0094 0.1417 0.0299
What we want to do here is switching the left and the right for echo: add left sound to the original right and vise versa.
Here is the script for mixing:
[sis, fs] = wavread('Alone-Sistar.wav'); % echo left-to-right and right-to-left! stereo_mix=sis; % set up a new array, same size as old one N=fs/2; % delay amount N/fs seconds for n=N+1:length(sis) stereo_mix(n,1)=sis(n,1)+sis(n-N,2); % echo right-to-left! stereo_mix(n,2)=sis(n,2)+sis(n-N,1); % echo left-to-right! end % audio save % wavwrite(s_echo, fs, 'sis_echo70.wav'); wavwrite(stereo_mix, fs, 'sis_stereo_mix.wav') % audio play % soundsc(sis,fs); soundsc(stereo_mix,fs); % plot time = (1/fs)*length(sis); t = linspace(0, time, length(sis)); plot(t,stereo_mix,'b'); xlabel('time(sec)'); ylabel('signal strength'); title('Sistar - Alone stereo mix');
Mixed stereo file: sis_stereo_mix.wav
If we play an audio at different sampling rare, we hear the sound if off:
[sis, fs] = wavread('Alone-Sistar.wav'); sound(sis,fs/2); % slow sound(sis,fs*2); % fast
This picture is the plot for left and right separately with red and blue:
The plot below is the case left minus right:
The script;
[sis, fs] = wavread('Alone-Sistar.wav'); sis_left = sis(:,1); sis_right = sis(:,2); % left - right sis_left_minus_right = sis_left - sis_right; % play sound(sis_left_minus_right,fs); % plot time = (1/fs)*length(sis); t = linspace(0, time, length(sis)); plot(t,sis_left_minus_right,'k'); xlabel('time(sec)'); ylabel('signal strength'); title('Sistar - Alone left - right');
As we can see from the plot for the mixed audio (1st plot), the left and right sounds are not exactly overlapped. In other words, the don't off-set each other and that's why we see the audio in the second picture.
Many ideas of my Matlab audio sections are from Sound Processing in MATLAB.
Matlab Image and Video Processing Tutorial
- Vectors and Matrices
- m-Files (Scripts)
- For loop
- Indexing and masking
- Vectors and arrays with audio files
- Manipulating Audio I
- Manipulating Audio II
- Introduction to FFT & DFT
- Discrete Fourier Transform (DFT)
- Digital Image Processing 2 - RGB image & indexed image
- Digital Image Processing 3 - Grayscale image I
- Digital Image Processing 4 - Grayscale image II (image data type and bit-plane)
- Digital Image Processing 5 - Histogram equalization
- Digital Image Processing 6 - Image Filter (Low pass filters)
- Video Processing 1 - Object detection (tagging cars) by thresholding color
- Video Processing 2 - Face Detection and CAMShift Tracking
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization