BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Matlab Tutorial : Vectors (Arrays) with Audio Files

Matlab_logo.png




Bookmark and Share





bogotobogo.com site search:




Introduction

In this chapter, we'll learn more about the vectors (arrays) while we playing with audio files. Our primary focus is on vectors but not on audio. Regarding audio, we'll have a chance to get more deep in later chapters.





Reading Audio Files

First, we need to read in the audio files using wavread() into arrays:

>> mantle = wavread('C:\SOUND\Clock_mantle.wav');
>> drum = wavread('C:\SOUND\Bogos_Drum.wav');
>> flag = wavread('C:\SOUND\FlagRaising.wav');
>> taps = wavread('C:\SOUND\Taps.wav');

The files are available:

  1. Clock_mantle.wav
  2. Bogos_Drum.wav
  3. FlagRaising.wav
  4. Taps.wav


bogotobogo.com site search:
Audio Signal - Segment, plot and play

Let's see what's in there:

>> mantle
         0
   -0.0000
         0
   -0.0000
         0
   -0.0000
         0
   -0.0001
   -0.0003
    ...
    0.0006
   -0.0009
    0.0002
    0.0018

The signal is quite long, and we can check how long it is:

>> length(mantle)
ans =
      302697

The audio sampling rate is 22050 (unfortunately, it's not CD quality 44.1 kHz), and we can calculate the duration:

>> dur = length(mantle)/22050
dur =
   13.7278

So, the duration is about 13 seconds. If we want to grab some portion (5-10 sec), we can use colon (':') like this:

>> rate = 22050;
>> m_seg = mantle(rate*5:rate*10);

We can plot the signal:

>> plot(m_seg)
mantle_seg_audio_signal.png

We can also play back using Matlab's built in function sound():

>> sound(m_seg, rate);

We can play it with higher rage, and if we double it or play it half of the recording rate:

>> sound(m_seg, rate*2);
>> sound(m_seg, rate*0.5);

Two Channel Audio Signal - New Plot

We may want to play with another sound and get more info while reading it in.

>> [d, fs] = wavread('C:\SOUND\Bogos_Drum.wav');
>> fs
fs =
       11025

This time, we get the sample rate, $f_s$ as well.

Plot the audio:

>> plot(d);
DrumAudioTwoChannels.png

Unlike the previous audio (mantle), now we have two channels:

So, we need to check the size:

>> size(d)
ans =
       88600           2

How about the previous one:

>> size(mantle)
ans =
      302697           1

Indeed! We had only one channel with the previous audio sample.

We want to separate the channels into two: left and right. Also, we want to use time as x-axis. The following script does it all:

left = d(:,1);
right = d(:,2);

time = (1/fs)*length(d);
t = linspace(0, time, length(left));
plot(t,left, t, right);
xlabel('time(sec)');
ylabel('signal strength');
DrumAudioTwoChannels_2.png



Two Channel Audio Signal - Play

Let's listen to the audio both in mono and in stereo:

>> sound(left, fs);     # mono
>> sound(right, fs);    # mono
>> sound(d, fs);        # stereo

Let's check the array:

>> left(10:15)
ans =
    0.0023
   -0.0052
    0.0231
    0.0658
    0.0559
    0.0604

>> right(10:15)
ans =
    0.0053
   -0.0115
    0.0527
    0.1541
    0.1322
    0.1362

>> d(10:15,:)
ans =
    0.0023    0.0053
   -0.0052   -0.0115
    0.0231    0.0527
    0.0658    0.1541
    0.0559    0.1322
    0.0604    0.1362

As we see, each column of d are composed of left and right column vector. Then, how we make d from the two column vectors?

d_new = [left ; right]?

The operation just append the right to the end of left, which makes it 2*length(left) x 1 matrix:

>> d_new = [left;right];
>> size(d_new)
ans =
      177200           1

>> size(left)
ans =
       88600           1

The answer is this:

>> d_stereo = [left right];
>> size(d)
ans =
       88600   

The audio is too short, and we want to hear it repeatedly. What should we do?

>> d_repeat = [d_stereo ; d_stereo; d_stereo];
>> sound(d_repeat, fs);




Reversing array

We can reverse a column vector using flip-upside-down, flipud():

>> cvec = [1;2;3;4;5]
cvec =
     1
     2
     3
     4
     5

>> rev_cvec = flipud(cvec)
rev_cvec =
     5
     4
     3
     2
     1

Using this reverse, we can play an audio reverse:

>> d_reverse = flipud(d);
>> sound(d_reverse, fs);






Matlab Image and Video Processing Tutorial

  1. Vectors and Matrices
  2. m-Files (Scripts)
  3. For loop
  4. Indexing and masking
  5. Vectors and arrays with audio files
  6. Manipulating Audio I
  7. Manipulating Audio II
  8. Introduction to FFT & DFT
  9. Discrete Fourier Transform (DFT)
  10. Digital Image Processing 2 - RGB image & indexed image
  11. Digital Image Processing 3 - Grayscale image I
  12. Digital Image Processing 4 - Grayscale image II (image data type and bit-plane)
  13. Digital Image Processing 5 - Histogram equalization
  14. Digital Image Processing 6 - Image Filter (Low pass filters)
  15. Video Processing 1 - Object detection (tagging cars) by thresholding color
  16. 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

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Matlab Image and Video Processing



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




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







OpenCV 3 -

image & video processing



Installing on Ubuntu 13

Mat(rix) object (Image Container)

Creating Mat objects

The core : Image - load, convert, and save

Smoothing Filters A - Average, Gaussian

Smoothing Filters B - Median, Bilateral






OpenCV 3 image and video processing with Python



OpenCV 3 with Python

Image - OpenCV BGR : Matplotlib RGB

Basic image operations - pixel access

iPython - Signal Processing with NumPy

Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal

Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT

Inverse Fourier Transform of an Image with low pass filter: cv2.idft()

Image Histogram

Video Capture and Switching colorspaces - RGB / HSV

Adaptive Thresholding - Otsu's clustering-based image thresholding

Edge Detection - Sobel and Laplacian Kernels

Canny Edge Detection

Hough Transform - Circles

Watershed Algorithm : Marker-based Segmentation I

Watershed Algorithm : Marker-based Segmentation II

Image noise reduction : Non-local Means denoising algorithm

Image object detection : Face detection using Haar Cascade Classifiers

Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts

Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods

Video : Mean shift object tracking

Machine Learning : Clustering - K-Means clustering I

Machine Learning : Clustering - K-Means clustering II

Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm










Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master