So, in this matlab tutorial we’re going to do some basic exercises just to get you working with image matrices and they’re going to be very similar to the exercises we did for sound. So the first exercise I want to do is how do we flip and image upside down? I’m going to give you a minute to think about that, so please pause this video and then come back when you have figured it out. Ok, so, we want to flip an image. It’s going to be exactly the same as we did for sound. So, if you recall sound was stored in using different samples in different rows, and essentially that’s what’s happening with an image also. Alright, so, the top of the image is the top of the matrix and the bottom of the image is the bottom of the matrix. So if we do flipud, which stands for flip-up down, of the image that we loaded and then we imshow this image, we see the original image flipped upside down. So now here’s something we couldn’t do with sound because sound is one-dimensional, so you can only flip it one way. Now let’s suppose I want to flip the image in the horizontal direction. How would I do that? So I’m going to give you a minute to think about that. Please pause this video and then come back when you figure it out. Ok, so, if you remember when we were talking about sound we looked at two different kinds of flip. There was flipud which flipped vertically, and fliplr which flipped horizontally. So all we have to do is call the other flip function. I’m going to imshow this flipped image, and then so this is the image flipped horizontally. And of course you could flip the image both vertically and horizontally to get this. Ok, so, now the next exercise we’re going to do involves working with color. So, I said before that the third dimension in the matrix are the red, green, and blue channels of the color. So how can we show this? So let’s say I only want to view the blue channel. How can I visualize that? So, I’m going to give you a minute to think about this, please pause the video and then come back once you’ve figured it out. Ok, so, usually we say RGB because red is the first component, green as a second component, and blue is the third component. So I’m going to assign the original matrix a to a temporary variable B. I want to view blue so what I want to do is I’m going to set red and green to 0. So how would I do that? I say colon to select all of the rows, another colon to select all of the columns, and then I put a 1 because I want to set the first channel which is red to 0. I’m going to do the same for the green channel, and so now only the blue channel has values that are not zero. So if I imshow B, now we see only the blue components of the image. Alright, and so if I imshowed one of the other channels that was set to zero, it’s just pure black. So 0 is black, it means no intensity at all. So, next we’re going to look at the green channel. So I’m gonna set G = A; G colon, colon, so I’m setting again red to 0, but now I don’t want to set green to 0 I want to set blue to 0, so it’s G (:, :, 3) = 0; and imshow(G), so here’s the green channel. So it’s a little bit more intense, or it has a higher intensity than the blue channel, we can see visually. So now let’s say we want to view the red channel. So we’re going to do something very similar to be above. So I don’t want to set red to 0 iIwant to set green to 0 which is the second channel, and I want to set blue to 0 which is the third channel. So imshow(R) and this is the red channel. So one thing to notice is that no matter which channel we look at, we can still pretty much just view the image. Now why is that? It’s because every color contains a red component, a green component, and a blue component. So, when you see colors think of them as sort of a mixture of those three.
Tag: programming languages
(FREE) Python Programming Course on Udemy!
If you want to learn how to program, you will LOVE this Udemy course! This course was designed for complete beginners with little to no understanding of programming, and will give you the knowledge to get started coding using Python 3.
Enroll for FREE on Udemy
We will cover the following topics in this course:
- Python installation
- Running Python scripts in terminal
- PyCharm IDE setup
- Numbers, strings, Boolean operators, lists, dictionaries, and variables
- Functions, arguments, return values, loops, and modules
- Final project using the information covered in the course
We hope you enjoy the course and it our goal to give you the knowledge to begin writing your own programs in Python!
Learn MATLAB Episode #17: Image Processing
Image processing in MATLAB
So, in this matlab tutorial we’re going to switch gears a little bit to a related topic image processing. The first thing I want to talk about is what is an image? So, we can compare it to soundwaves since we’ve already studied those. Now let’s think about the most raw data possible. So, firstly, whereas sound is a one dimensional function, images are two dimensional functions. We have an x, a y, and a z, and so that z is the value at that x and y. In fact, you can think of a heat map or a contour map as basically images. So, we have a 2d function, ok, x & y are continuous coordinates in space. And like with sound we have to sample the data in order to store it in a computer. So, with sound we’re sampling across time and we call that the sampling frequency. What’s analogous to the sampling frequency when we’re dealing with images? I’ll give you some time to think about that, this is actually probably pretty familiar to you. So, with images the analogous concept would be the resolution, right, so the amount of fine-grainess you put into each point in space. For example, Udemy videos are required to be recorded in HD resolution. So, now let’s suppose we have an image called a, a will have the indexes i and j referring to be xy coordinates. Just write this down if it helps you. So, we have an image a, and then a(i,j) would be the xy coordinates. So, now let’s think about what would be stored at a(i,j). In other words, how do we store a pixel? So, now let’s suppose we store just one number at a(i,j), so it’s a two-dimensional matrix or a table. A table of values at each point xy. That image would be, that matrix would represent a grayscale image, right, because if we only have one number we can only represent intensity. So, 0 would be black and 1 would be white, and so a(i,j) would be the intensity at the point ij. In order to store a color we need three parameters r, g, b, or red, green, and blue, and this is what we’ve been looking at for the entirety of this video so far. So, these values usually vary from 0 to 255 so we can have a total of 256 to the power of 3 possible different colors. So, let’s look at what the value of that is. So, that’s about 16.8 million different colors we can represent if we store values from 0 to 255 for RGB. Now what this also means is that if we have a 500 x 500 image, the matrix that we use to represent it isn’t 500 x 500 but it’s 500 x 500 x 3. Sometimes this is called an image bitmap. Alright, so now that we’ve talked about images in the abstract sense, let’s talk about how you would actually open an image in matlab. There’s an image processing tool box that we won’t be using a lot of in these tutorials. Matlab has a function called imread built-in. We’re going to use that function to read in a very famous picture often used in image processing, so if you ever take a computer vision course or multimedia course you’ve probably seen this image before. Ok, so, let’s look at the size of a. So, it’s 512 x 512 x 3, so a three dimensional matrix as promised. Notice the data type is uint8 instead of double that I have for my other matrices which is the default in matlab. So, a unint8 the u stands for unsigned, int stands for integer, and eight means it’s 8-bit. Ok, so, we think about how many values could be stored in an unsigned 8-bit integer let’s go ahead and calculate that. You should pause this video and try to think about the answer yourself first. Ok, so, the answer is 2 to the power of 8, and that’s 256. So, as we discussed previously values for R, G, and B go from 0 to 255 which is 256 different values. There’s a function called imshow that will essentially plot the matrix as an image. So, that’s the image, the famous Lena image, that’s used for image processing.
Learn MATLAB Episode #16: Low Pass Filters
In this tutorial we’re going to talk about low pass filters, and how to apply those to an audio stream. So, to first recap and kind of give you a different perspective on what we’ve been doing, we’re just going to listen to a sound wave at different sampling frequencies. So, I’m going to create my x-axis (0, 50*pi,10000) samples; I’m going to say y = sin(x); So, this sound is only at one frequency, so you’re it’s going to sound kind of like a buzz. So, I’m going to play the sound at a common frequency that’s used for recording mp3’s and waves 44100. So, let’s listen to this. Ok, and so now what I’m going to do is I’m going to play the same sound wave but at a lower frequency, or sorry a lower sampling rate, and that should give me what sounds like a lower frequency even though it’s the same wave, the same matrix of values. And so it indeed sounds lower pitched, and so now what we’re going to do is we’re going to create a low-pass filter. So, what that does is say we have multiple different sine waves playing, which is essentially what the Fourier transform is giving us, we’re going to filter the signal so that only the low frequency components can be heard. So, if we look at the typical shape of a low-pass filter you generally see things like this, and remember that this is only the left half of the plots that we’ve been looking at. When we’re in the frequency domain convolution turns into multiplication. So, right here zero decibels is actually equal to 1 because it’s a log scale, so we’re multiplying 1 by the signal at these low frequencies, but then as we go down here we’re multiplying a very small number by those higher frequencies. And so after we’ve multiplied one signal by the other one of them being this low pass filter, those high frequencies are going to be silenced. So, if you’ve never studied about low-pass filters before you may want to go and look up the different kinds, what’s their function representation, and the time domain and the frequency domain. We’re going to use a very simple version of the low-pass filter called the moving average. So, matlab already has a function for us that can create filters, we just need to pass in the b and the a which is essentially the different multipliers on the original signal. So, I’m going to set b = ones (40,1)/40; I’m going to call…I’m going to load my data first. [d,fs] equals audioread(‘helloworld.wav’); So, I’m going to say dlp = filter(b, 1, d); so I want 1 as the numerator and then b as the denominator, pass in the data. So, now I want to play the original hello world for reference “hello world” and I’m going to play the new low pass filtered version to compare “hello world” So, you can see the, or you can hear, the low-pass filtered version sounds very muffled, sounds like you’re speaking through a pillow or something, and that’s because high frequencies tend to not go through different medium, so low frequencies can go through a wall. So, if you hear your neighbors talking for instance it will sound a little muffled, and that’s the same thing that’s happening your wall is acting like a low-pass filter. So, let’s do another way of comparing these, I’m going to plot them on the same axis. It’s convenient because they’re the same size, ok. So, now if I zoom in a little bit..so you can see all the sudden changes in the dark green signal don’t happen in the light green signal which is the low-pass filtered version. That’s because when you see sudden changes that’s actually you can think of it as a very fast sine wave that’s super positioned on a low-frequency signal, and so we’re essentially just getting rid of those. Now yet another way to compare these two signals is to plot them in the frequency domain. So, for this we want to use subplots so that they do not overlap each other. Let me just do it all in one line…ok. So, remember that the original signal is on the top and the filtered version is on the bottom. So, now you can see that we’re keeping the low frequencies, but the high frequencies which seemed to have some components that are nonzero are all damping or they’re set to zero in the filtered version. Now if you’re interested in this what I would recommend is an exercise is to try to implement a high-pass filter. As a hint you’re going to want to use the same function filter, and everything else is going to stay the same.
MATLAB Programming Tutorial
Limited FREE coupons 🙂
http://bit.ly/2kmmttg
$10 Coupons on Udemy
http://bit.ly/2jPlZuG
I hope you enjoy this FREE MATLAB programming tutorial. This video is a compilation of lectures from MATLAB from A to Z: From Programming to App designing on Udemy.
Topics covered
- MATLAB user interface
- Basic commands
- Using help topics in MATLAB
- Introduction to programming in MATLAB
- M-file scripts
- M-file functions
- Input and output commands
- Accessing the guide
- Available controls with their types and generated files
- Properties of controls (initial values and tags)
- Positioning and aligning controls
- Created functions in the .m file
- Two key functions
- Layout of app designer – design & code view
- Available controls and their types
- Alignment and arranging options
- Spacing and resizing
- Grid lines
Learn MATLAB Episode #15: Fourier Transform
So, in this matlab tutorial we’re going to extend what we learned last time about the Fourier transform. So, last time we took the Fourier transform of a sine wave, and if you’ve ever studied the Fourier transform in school you know that when you take the Fourier transform of a sine wave you should see a delta function or a spike in the frequency domain, and what we were seeing was something a little strange. So, suppose I set my x to linspace(0,5*pi,1000); and then I plot(real(fft(sin(x))) Ok, so, what we see here is actually two spikes not one spike, and this is due to artifacts, due to sampling, we don’t have an infinite sine wave which is assumed when we calculate the fft of the sine wave, and then we get a delta function in the frequency domain. So, what we could do if you want to have closer to an internet sine wave you can do 0 to 10 pi, so that’s five full sine waves, and then do 500 samples. Plot sine of X again, and so now you can see something much nicer. So, now I want to extend the idea of the Fourier transform a little bit. So let’s say we’re doing the Fourier transform of a song or somebody playing the piano, the frequencies if you’ve ever studied music you know are at certain notes, are multiples of each other, and so the frequency is going to change as the song progresses. Now how do we show that using the Fourier transform? The Fourier transform takes the entire signal from time equals minus infinity to infinity, and so what we’re really doing is we’re taking all the different frequencies over all of time and then plotting that on the same Fourier transform. And so the answer to that question is we use something called the short-time fourier transform. And so the way that works is it’s a two dimensional heat map, or you can think of it as a contour plot or something similar, that shows time on one axis and frequency on the other axis. It shows you different frequency components at different times, and the way it works is it takes the Fourier transform of small windows of time and then moves it along the signal as time goes on. So, it’s only showing you the frequency spectrum of a small window at each time. Another name for the short-time fourier transform is the spectrogram. So, if we look here there are different function signatures that you could use. Notice that we require the signal processing tool box if we want to use this method. A spectrogram look something like this, you have time and frequency and it shows you how the frequency, the main frequency component is changing as time goes on. So, how do we create, let’s say we want to test this function out, how do we create a function that changes frequency as time goes on? So let’s say we set X as before, actually have already signed it, so it goes from 0 to 5pi or 0 to 10pi, and it has 5,000 points. So, suppose we set the frequency. So, a wave looks like this, frequency*time. So, suppose we set the frequency Omega, and I’m just typing this out this isn’t real matlab, frequency as a function of time is just equal to the time. So, we’re going to increase the frequency linearly as time goes on. So, if we combine those two we get something like this, y2 = sin(x.*x) because this is element by element multiplication. Ok, so, now I do spectrogram of y2, and you see that the frequency is increasing linearly, so it takes an equal size step at each window, and notice that there are some artifacts due to the fact that the frequency is not equal within the window that we’re calculating the fft. So, you notice that it looked kind of blockage, what you can do is you can pass in the window size along with the data. So, let’s try window size of 100, so now you can see it looks much more like a line. So, let’s try that again with the window size of 500. So, now you see the little red line is a little bit skinnier but the time windows are a little bit bigger, and so this is what we call the uncertainty principle and you may have heard this if you take in quantum mechanics. The uncertainty principle when you measure the momentum and it’s very accurate, you can’t measure the position as accurately. So, it’s the same when you’re taking the Fourier transform because if we want to know the frequency very accurately we need to have a very long window of time, but that gives us different frequencies at a lot of different times. If we want to know the frequency at a very specific time we have to shorten the window, but then we have a frequency that’s not as accurate.
Learn MATLAB Episode #14: Signal Processing
Let’s do some signal processing exercises in MATLAB!
So in this tutorial we’re going to go a little bit deeper, and more general then just sound, and we’re going to talk about signal processing. So, one of the most important concepts in signal processing is the Fourier transform. If you’ve never heard of this you can go to Wolfram.com and look up Fourier transform. So, if you’ve never taken calculus or some sort of advanced university math this probably won’t make a lot of sense to you, but if that were the case you probably wouldn’t be doing this course in the first place. The Fourier transform is essentially in a more of a conceptual way it lets you view the frequency components of a signal. So, you convert a signal from the time domain into the frequency domain and then you can use the reverse Fourier transform to go from the frequency domain back to the time domain. So, now if you look at these equations you notice that f(x) the original signal is a continuous function of X, and so we call this the continuous Fourier transform. Now since we’re working in matlab and we’re working with arrays and matrices that won’t really work for us. So, we need a sort of discrete version of the Fourier transform, and appropriately it’s called the discrete Fourier transform. So, let’s look that up. Ok, so, we’ve sampled f(x) or sometimes we call it f(t) since you know if we’re looking at a sound signal its varying in time. So, we sample f(t) at a certain frequency delta, so that’s the sampling period, and then we assign those values at each sample to f(k), and so f(k) becomes a discrete signal. One thing to note from last time that we talked about sampling and quantization, so this is sampling only not quantization. So, once we have f(k) we can then calculate the discrete Fourier transform f(n). So, this brings us to the next concept is what’s the function in matlab that actually calculates the discrete Fourier transform? So, there is an algorithm called the fast Fourier transform, also known as FFT for short, it calculates the discrete Fourier transform in n log n time. So, if you were to, if you’ve studied algorithms you know about o(n) or Big O notation, so if you were to calculate the Fourier transform naively you would get an o(n^2) algorithm which is slower than and n log n. We don’t need to worry about the details of the algorithm just that matlab has a function called FFT. So, as an example let’s do sine wave. So, let’s calculate the FFT of sine wave…so now let’s try to plot the FFT, or the transform signal. And so you’ll notice it looks kind of weird, this is not what we want to see, so this is just an intermediate step that I’m showing you. So, if we go back to the definition of the Fourier transform, let’s look at the discrete Fourier transform, you’ll see that it’s the original signal f(k), times e to the power of -2, pi, i, nk/N. So, the exponent to the power of i where i squared is -1 is going to give you a complex number, so there’s going to be a real part and an imaginary part. You can use Euler’s equation to see this more easily, cos(theta) + i(sin)(theta), so there’s a real part and an imaginary part. So, if we look back at big Y which is the transform signal, you’ll see that each of the parts here has a real part and an imaginary part. So, typically when we’re plotting the FFT we usually just look at the real part. So, there’s a method called real in matlab, so suppose I have some number 2 plus 3 times i,, real will just drop the imaginary part. So, now I’m going to plot real times big Y which is the FFT of little y. Ok, and you can see that there are two spikes, one on the left and one on the right. So, when you’re looking at the frequency domain the Fourier transform you really only need to pay attention to the left half. So, this area is the lowest frequency, this area in the middle is the highest frequency, and because you look at the Fourier transform again there’s this 2pi in the exponent, and also that it’s a complex number which then resolves to cosines and sines. The Fourier transform’s actually periodic, so it’s periodic in 2pi, and so when we look at the continuous Fourier transform you’ll see that the signal actually repeats every 2pi. So, when you look at a signal, so let’s just look at and see if we can find one, this is a good picture what I’m trying to explain, so usually the way that the Fourier transform is visualized is that it’s symmetric around the center. So, we only show from -pi to pi in the frequency domain, and then anything you see on the left side of the zero-point here it’s just carried over to the far right when you’re looking at the discrete Fourier transform. Another thing to notice is that the transform signal is the same size as the original, so size big Y is the same as size little y. So, the important thing I want you to grab from this is that the sine wave has only one frequency, right, and so that’s what we’re seeing here, that’s what this spike is. Now so when we look at real signals,, so we’re going to look at some voices soon, what we’re going to see is there are going to be multiple frequency components, alright. So,, when you speak or when you play an instrument they produce different sounds because they have different frequencies playing at the same time. So, we’re going to go back to the hello world from the first and second lecture. So, audioread helloworld.mp3. So, remember that d has two channels, so we’re just going to look at the left or first channel in the FFT lectures. So, I’ll say big D is equal to FFT of little d, so I’m going to select all the rows and then the first column. Now I’m going to plot the real part of the FFT(D). Alright, so, you can see the multiple frequency components that show up this is my voice. So I’ve downloaded a female version of hello, I couldn’t find hello world so she just says hello, going to read that in. So, I’m going to play it so you know what it sounds like…”hello.” Okay, so, the female voice is more high pitched than the male voice, so mine is a male voice. So, what we should see then I’m going to take the FFT of the female voice, I’m going to plot that, so this doesn’t tell us much because it’s just a plot by itself. So, we’re going to use some of the things we learned before, okay, I’m going to use subplot. I could try to plot these on the same plot but we have a problem, right, so we look at the size of D and it’s about 85k, and if we look at the size of F it only 8,000, so it’s a much shorter “hello.” So, the problem with that is we plot them both on the same axis one is going to be really short and the other one is going to be really long so you can’t really compare the frequencies that well, so what we’re going to do is we’re going to use subplot. I’m going to plot the female version up on top, and I’m going to plot the male version which is me at the bottom. Ok, so, remember that the signal is symmetric, right, so I only have to pay attention to one side, and they’re relatively the same length on these plots. So, now notice that the female voice takes up higher frequencies and my male voice takes up lower frequencies and its loudest on a very low frequency. So, the Fourier transform can be used to analyze sound signals or any kind of signal that you want to know the frequencies of.
Learn MATLAB Episode #13: Sound Processing Exercises
Let’s cover a few sound processing exercises in MATLAB!
So in this matlab video we’re going to do some exercises related to sound processing. So the first exercise we’re going to do is try and reverse the audio file, please pause this video and see if you can figure out how to do it yourself first. Ok, so, if you search on Google how to reverse a signal you see two functions that pop up, there’s fliplr and flipud. So now if we look at the sound file that we opened earlier, hello world from the last lecture, we see that it’s about 85,000 x 2. So this tells us that the samples are along the rows and each column is the left and right channel. Therefore, we want to use flipud not fliplr, because if you fliplr it’s just going to reverse the two channels, but if you flipldd it’s going to reverse the signal. So, let’s do that. So you see the shape is the same. If I plot d2 it’s not apparently obvious but this is the reverse of the signal that we had before. So now I want you to play this signal. Ok, so, I want to play this signal for you guys, but I have to save it first so that I can put it into the video editor that I’m using. I’m going to skip ahead a little bit and talk about the function audiowrite. So this is analogous to audioread, you pass in the data, and then you pass in the sampling rate. Ok, so, matlab does not support mp3 in audiowrite for some reason even though you can read mp3’s, let’s try mp4. Ok, so, now I’m going to read this file, I’m going to get the sampling rate also, going to play this back. So, that was hello world backwards. So the next thing I want to do is I want to speed up the sound waves so it plays twice as fast. I’m going to give you guys a minute to think about that, and so please pause this video and then come back once you think you’ve figured it out. Ok, so, the process of speeding up a sound wave is called downsampling. So we’re going to use an integer number like 2 so that it’s a little bit easier. So conceptually what you want to do is you basically want to drop every other sample of the original signal. There is some other algorithms and functions you could apply that theoretically might give you a better sound, but just to take every other sample is the simplest way. And so if you search for this earlier there is a method called downsample in matlab, ok. And so notice that we check the size of d4 the signal is half the length that it was before because we took every other sample. Now I want you to play this back with the original sampling rate. And so now it sounds like a chipmunk because it’s going twice as fast, and the pitch has increased. So if you think about a sine wave that doubles it’s frequency that’s the exact same thing that’s happening here. And now the next exercise I want you to do is theoretically maybe a little bit more complicated, but I think you guys can get it. So now that I have this downsampled version of hello world that’s playing every other sample of the original, how do I get it to play at the original speed so that it’s not high-pitched? I’m gonna give you guys a minute to think about that, so please pause this video and then come back once you think you’ve figured it out. Ok, so, the solution to this what we want to do is we want to play at half the sampling rate. “Hello world” and so this sounds pretty much like the original, you probably can’t tell that there’s some information loss, this has to do with a thing called compression. So if you ever study multimedia and you look into this field further you will learn more about that.
Learn Matlab Episode #12: Sound Processing
Click here to subscribe for more videos like this!
How do you open and process sound files in MATLAB?
In this tutorial we are going to talk about sound processing in matlab. For this tutorial there are no toolboxes needed. So the first thing we’re going to talk about is what sound is. So sound, as you may have heard, is a wave and it’s a wave of air particles, and so when you think of a wave usually we think of something like this like a sine wave, we call that a transverse wave. When we think of sound though it’s a variation in pressure right that bounces off your eardrums, so a wave like this doesn’t really make a lot of sense. There’s another kind of wave called a longitudinal wave, and this is the kind of wave that includes sound. So if we look at this visualization of a spring this describes what is happening to air as sound travels through it. Now interestingly enough when you record sound on a microphone and convert it into an electrical signal, that signal can be viewed as a wave like this. So, we again get a transverse wave. And so when we look at sound in matlab we’re going to be looking at a transverse wave even though in reality it’s a longitudinal wave. So, one thing you have to remember is that an electrical signal is an analog wave, and when we represent things in matrices on computers those are digital signals. So, for example, you could have a sound recording of say two seconds long and then that translates into a matrix of length 1000, so that would mean you have 500 samples per second, and so this process of sampling is what we call discretization. And so sampling is the discretization of data points along the x-axis, or in this case time. The other kind of discretization we have is along the y-axis. So, this is because numbers in matlab, numbers in any programming language, can only take on certain values. So you can’t have an infinitely precise number. This is because numbers are stored in quantities of say 32-bit or 64-bit and so we have to not only discretize the signal in time along the x-axis, but we also have to quantize the signal on the y-axis. And so both of these lead to some error, but in general say you’re playing a WAV file or you’re playing mp3 you don’t really notice these differences, they are imperceptible to the human ear. So now that we’ve covered a little bit of the theory behind what sound is and how it’s translated into a digital signal that we can read in matlab, we’re going to go ahead and open a sound file in matlab. But, before I do that I’m going to show you how I created the sound file. So there’s a free program on all major platforms linux, windows, and mac called Audacity. It’s free and open-source, you can download it just by searching on Google, and so if you have a microphone connected to your computer you can record things. So I’ve recorded myself saying hello world, hello world, and I’ve saved it to a WAV file. I have this WAV file in my workspace, and so there used to be a function in matlab called the wavread that you can use to extract data from a WAV file. Now notice I get a warning when I use wavread. When you search on Google for how to open a sound file this is the thing that’s going to come up. So, you’re going to get this warning and wonder what’s going on. So wavread is going to be deprecated and there is a method called audio read that’s going to be used instead, but this is currently not showing up in Google results. So let’s look at wavread first. So it only reads WAV files, and so if you don’t know a WAV file is just raw digital sound data. So we sample the sound, and we quantize it, and then we save each of those data points as an array essentially into the file. So there are different method signatures that we can use with wavread, we can get just the data which I’ve done or we can get the data and the sampling rate. So commonly we use FS to refer to the sampling rate the, F stands for frequency, S stands for sampling, so the sampling frequency. Now there’s some interesting things we can do once we have the data. So I can plot the data, not sure where it went, there we go. So you can see here it pretty much looks the same as what I had in audacity, also check out the size of the matrix. So there are 84,480 samples just for that maybe one second of data, and notice that the other dimension here is two. If you think about why that is let’s go back to audacity, there are two channels. So I was recording in stereo right, so there’s the left side, and the right side, so that’s why there are two dimensions to this matrix. Now another interesting thing you can do is play sound. So I can actually play this file back but for that I need to so there’s a method called sound in matlab that you can use to play sound, but we need to use a different method signature for a wavread in particular. We need to get the sampling rate. So I’m going to use wavread hello world and return both the data and the sampling rate. So I do sound, pass in the data, and pass in the sampling rate, “hello world” and it plays back the sound. So now you might be wondering what will happen if I try to read a mp3 file. So I’m going to save this as a mp3, the exact same file, so keep in mind that wavread of course only works with wav files. If we want to read mp3’s or any other kind of compressed sound file you should use audioread, the new function for reading audio in matlab which can also read waves by the way. Okay, so now let’s say one play this “hello world” and so it sounds exactly the same.
Learn Matlab Episode #11: Subplots, 3D Plots, Labeling Plots
[embedyt] http://www.youtube.com/watch?v=AvJbKHQTI5U[/embedyt]
Click here to subscribe for more videos like this!
Creating subplots, 3-D plots, and labels in MATLAB
So in this lecture we are going to talk about more plotting. We’re going to talk about subplots, so that’s putting more than one plot into the same figure, we’re going to talk about three-dimensional plots, and we’re going to talk about how to label plot, so you want some field for the x-axis, the y-axis, and the title. So, let’s go into subplots. So suppose I have some
function of X, so let’s just say Y = sin(x), then I have another function which is just Z = Y + randn(1,100); Ok, so I can plot these individually, I can plot them together, I might want to stack them up or something to see if there is a correlation between them, so the way we do that is with subplot. So we call a function called subplot and it takes in three values. Ok, so the three values are the number of rows in the subplot, the number of columns in the subplot, and then which subplot you’re about to plot. So since I have two subplots let’s say I want two rows in one column and I want to set the first plot. So I just called subplot(2,1,1); and I say plot(X,Y); Ok, so now my figure has X,Y in the first row and first column. So now I do subplot(2,1,2); because I still have two rows in one column, but now I want to plot something in the second slot. Here I’m going to plot X and Z. So now if I look at my plot you can see the two plots stacked up on top of each other instead of overlaid. So the next thing we’re going to talk about is 3D plots. So suppose we have some two dimensional data set I’m just going to make it random noise. Okay, so we look at the matrix Z, we see that it’s 100 by 100, so the rows you can think of as the x-axis, the columns is the y-axis, and then the value as the third dimension. So how would we plot this? So there are a couple ways you may have seen already from your math courses, so we can plot an actual 3D plot so sort of how you would draw a cube on a two-dimensional piece of paper, or you could use what’s called a contour plot which draws lines where the value of the function is equal. So we’re going to do both of those. The first one is a function called surf, so you pass in the matrix and so you can see the x-axis and the y-axis both go from zero to a hundred. If you look up the documentation you can set the x-axis and y-axis manually, and then the height is the value of the function. So what I did just now as I clicked on the this thing that sort of looks like a circular arrow and so what that allows me to do is it allows me to rotate the surface plot, so now I can look at it from different angles which could be very useful if you’re doing data analysis. The other type of plot that we talked about is the contour plot. So the simplest way to call that is with the one argument and that generates just a 2D visualization of your data, the red is the really high values, and the blues the really low values. So the third thing we’re going to talk about today is how to label plots. So far we’ve only seen plain visualizations, so let’s go ahead and plot the sine function again. Alright, so I want to give this plot a title, all I do is use the title function title(‘A plot of sin(x)’) Ok, so now if I look at my plot you can see it now has the title A plot of sin(x). Now I want to label the x axis I’m going to label it with the very unoriginal label. Ok, so now you can see the x-axis is labeled and you can do a similar thing for the y label as well. Ok, so that’s pretty much it for labeling, suppose I want to label a subplot. Ok, so now I’m going to create another function, going to subplot(2,1,1), plot(X,Y), title(‘sin(x)’) Ok, so once I do that the first plot, first subplot gets the title sin(x). Now I call subplot(2,1,2), I plot(x,z) and I call title again so it’s the same function but it’s stateful so it knows that I called subplot earlier and that it corresponds now to the second plot. Ok, so I call title again and now my second plot has the title cos(x), and my first plot has the title sin(x).