MATLAB is much easier to learn when you can try everything for yourself in this course for beginners! With more than a million users, MATLAB is a must know programming language for science, engineering, and economics professionals.
Learn about solving equations in MATLAB, data structures, probability, and how to plot data in MATLAB from a software engineer with proven experience using MATLAB. If you want a screencapture course that shows you exactly how to use MATLAB, you’re ready to take this course!
Now we’re going to move on to the next step in order to implement our blurring tool, or our blurring filter. So the first thing we need to do is we need to extend this idea of convolution to two dimensions. So, the first modification we did so we looked at convolution at it’s most basic definition where it’s of a continuous variable filtered by another continuous variable, so we discretize it by turning it into a sum, and so we already have discrete signals because we’re working in matlab. Now, when we do two-dimensional convolution notice we now have two dummy variables. So before tau or k, now we have two dummy variables tau 1 and tau 2, or n1 and n2,. And so it’s basically what you would expect when you have one deconvolution you’re going from minus infinity to infinity along the one dimension that’s the independent variable, so you can think of that as time, and in two dimensions we go from minus infinity to infinity for both independent variables so that the x and y, or in other words the two spatial dimensions of the image. So now that we’ve extended our idea of convolution to two dimensions let’s think about how we could implement a blur using a Gaussian. So a Gaussian is basically a spread, right, there’s a middle point and then it spreads out over a radius in a circular fashion. So, my question to you is how can we build a two-dimensional Gaussian image that we can use as a filter on the original image, so that we can do a convolution between those two? So, I’m going to give you a minute to think about that, please pause this video and come back when you have figured it out. Ok, so, I have in fact actually been showing you the solution to this for multiple videos now. So I’ve created a function called my Gaussian, and it takes in two parameters n and Sigma. So n is going to be the size of the square, so the output is going to be an n x n matrix, and Sigma is going to represent the standard deviation of the Gaussian as is convention when we’re talking about Gaussians. So we have the output called H, now I only need the value of two Sigma squared ever in this equation, so I’m just going to calculate 2 sigma squared at the beginning so I don’t have to do that on every iteration of the loop. I’m going to initialize h to be an n x n matrix of all zeros, and then I’m going to use a for loop to assign every value of age. So, I going from 1 to n, and j going from one to n. Now I assign x to equal i minus n/2, and y to equal j minus n/2. Why is that? Because I want the center point of H, so that would be H of n over 2 over 2, to to be the highest point of the Gaussian, and so that’s when x is equal to 0 and y is equal to 0, and the exponent of 0 is 1. So that would be the maximum value of the Gaussian and then every point from there would be smaller, right, so when I is n/2 x=0, when j is n/2 y=0. And the formula for a Gaussian is x squared plus y squared over 2 sigma squared, and then you take the exponent of the negative of that. Ok, so, what does this actually give us? So let’s use the my Gaussian function, say n is a hundred and Sigma is 10. Now let’s imshow the H that I got. Ok, so you can see so remember that white is the maximum value 1, and black is the minimum value zero, and so this is a Gaussian what a Gaussian looks like when you plot it on an image. So what would I do if I wanted to see more white? I could increase Sigma, right? So, let’s say Sigma is 25, imshow H, and so you see the radius of the white part has increased.
In this matlab video we’re going to talk about convolution. So I mentioned this before when we were talking about the low-pass filter, because they are very similar and related concepts. So, when we’re talking about the low-pass filter we did a very simple filter called the moving average, and so to give you a sense of what that’s doing again you’re taking a window, say five samples at a time, and then you’re sliding that along the signal and taking the average, and then that is the output of the filter. So this sliding motion and then applying some function to that window that’s sliding along is called convolution. So what you’re really doing when you’re applying a filter is you’re convoluting one function with another. So let’s look at the definition of convolution. OK, so, convolution is also known as the star operator, and it’s the integral of one function with this dummy variable. So you can see this sliding motion that I was talking about. Now this will not make a lot of sense to you if you haven’t studied calculus before, but there is one important result from convolution, or the study of convolution, that we should talk about and then you should know, and that is that convolution in the time domain, and we use time as a sort of dummy variable so time to mean actual time, or time can mean space, the important distinction is that you’re going from time in one domain to the frequency in the other domain, so the result that’s important is that convolution in the time domain, so if I convolve one signal with another, this is equivalent to multiplication in the frequency domain. So, what is the significance of this? So that means they’re two equivalent ways of computing the convolution of the signal with its filter. So one way is to just do the convolution using the formula for convolution which is here and of course in matlab it would be a sum not an integral since we have to discretize the signals, but the other way we could do is since convolution in time is equal to multiplication in frequency, we could take the Fourier transform of both signals first, multiply them, and then do the inverse fourier transform to go back to the time domain, and so that would be equivalent to doing convolution. One application of this is the Fourier transform, or the Laplace transform, can be used to solve differential equations. So you can solve them in the frequency domain and then convert that to the time domain to get the signal of interest back. So, now again when we think about convolution a very useful analogy is this sliding motion, and so that’s exactly what the moving average was doing. And so one physical manifestation of this is the blurring of an image, and so you’ll see in a later lecture that I’m going to do that the blurring of an image is actually convolution, but the sliding motion you will actually do you cando by hand and it has same effect. So this is to say if you’ve ever used photoshop and you use the blur tool on an image, you notice that you click on the blur tool and you get a point, and then you slide it across the image on the places where you want to blur, and then it blurs those points.
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.
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.
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.
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.
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
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.
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.