Posted on

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.

Posted on

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.

Posted on 1 Comment

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.

Posted on

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.

Posted on

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).

Posted on

Learn Matlab Episode #10: Loading & Saving Data

Click here to subscribe for more videos like this!

How do you save & load data in MATLAB?

So in this tutorial I’m going to talk about loading and saving data. So a lot of the time when you’re doing data analysis your data is going to be passed around and files that you might download off the internet are stored on your hard drive or something like that, so you’re not going to be manually typing in matrices into matlab all the time. The first thing I want to talk about since it’s probably going to be really popular with Windows users is excel. So Excel uses a format called .xls and there are two functions in matlab that are useful for reading and writing .xls files. Ok, so if you type in help xls read you can see the documentation for the xls read function. There’s also a function called xls write that you may want to look into. So the reason I’m not going to cover xls read and write in this tutorial is because first of all it’s not perfect. So, I saved I don’t I’m on a mac so I used Google sheets to export and .xls document and it’s a very simple document, it’s just the three by three matrix with the numbers 123456789 in matlab. So xls read matlab example gives me an error when I’m trying to read this file, so we’re not going to be working with .xls. In addition to that, so if you’ve ever participated in a Kegel contest, or you get a file to work with from school, I’ve never seen anyone ever use .xls. Usually it’s a .csv, sometimes it’s a .mat file which is a matlab file. So we’re going to be working with those. So .csv I would say is the most common, it stands for comma-separated values, so your file will look something like this 123456789, and I’m going to save this CSV example .csv. So we have a function called csv read csv example, ok and so that did exactly what I wanted it to do. So you can see how the csv file pretty much one to one corresponds with the matrix that you would get after you read the file. Now let’s save a new matrix, let’s create something a little different. Ok, so just fives along the diagonal. I’m going to call csv write the another.CSV, so I did it backwards because I didn’t know. So I’m going to write the matrix B to a file called another.CSV. So if I look at this file…alright so when I double-click it matlab gives me kind of a CSV editor. So now what I’ve done is I’ve opened the same file in a text editor on the command line console and so you can see again what we’d pretty much expect a CSV of the matrix that we made. So that’s the nice thing about CSVs is they don’t just work with matlab they work with many other types of programs, it’s just a plain text file. So now the other way that we can read and write files is the .mat file which is matlab specific. So I’m going to clear my workspace, you’ll see why. I’m going to create one matrix A = [1,2,3,4], oops. Going to create another matrix B = [5,6,7,8] Ok, so now i have two matrices A and B so now what matlab lets you do is it allows you to save your entire workspace all into one file, and so all I do is I call save my workspace.mat, okay. So now if I look at this file, so I open this .mat file in a text editor, notice how it just shows me a bunch of garbage. So this is binary data specifically for matlab, so that’s why you might prefer to CSV so you can be cross-platform or cross program. So I’m going to clear my workspace again and I’m going to call load my workspace.mat and so what load does is it just reloads your entire workspace that you saved into the .mat, and so that could be useful if you don’t want to save every variable individually into their own CSV. What you can do is you can if you don’t want to save all the variables you can pick specific ones so let’s say I create another matrix C = [9,10,11,12] Ok, so I have variables A, B, and C but I only want to save A and C, so I’m gonna give it a file name AC.mat, I’m going to pass in so you can’t pass in the variable you have to pass in the name of the variable, so I’m going to pass in A and C to save it, I’m going to clear my workspace, and so now when I load AC.mat it just gives me back A and C only but not B.

Posted on

Learn Matlab Episode #9: Data Analysis Plots

Click here to subscribe for more videos like this!

What are the best plots for data analysis in MATLAB?

So in this tutorial we’re going to be talking about different types of plots that could be useful for data analysis. So the first type of plot if you’ve ever used Excel or watched a PowerPoint presentation, you’ve probably seen this probably seen most of these actually, so the bar plot. So suppose we have some sequence of data let’s, just say x equals 1 to 10, and we want to plot this on a bar chart. We just call the method bar, pass in X, and we see the bar chart. So this does pretty much the exact same thing as a plot function except instead of connecting them all in a line it connects well it doesn’t connect the points at all it gives them each their own individual bar. So, just for reference let’s do let’s do our sine example again. Ok, so we’re going to do bar( X, Y), right, so this is a bar chart of the sine function. Ok, so the next type of plot that I want to talk about is the histogram. So if you’ve ever seen, if you’ve ever studied probability you know about probability distributions and given a data set we might want to figure out what kind of distribution does it follow, so let’s generate some data where we know the distribution. So I’m going to create a thousand points that are normally distributed. So if I plot this it doesn’t do much it just looks like noise, so I won’t be able to see much from that. What you want to do is you want to call a function called hist which will automatically create a histogram out of your data. So I’m going to call the simplest version which is hist of just the data right, and so you see that it’s pretty much normally distributed. There’s a little bit more weight on the right bar than the left bar in the center and so the resolution for this chart is not that great. So what you can do is you can set the number of bins right, so if you have a lot of data, in our case a thousand points, you can set the bins to maybe say 50, ok, and so this gives us a much more granular representation of the data. So now we can see it still looks normal, you know there’s a little less data than expected kind of right here at zero, and then a little more to the right of that. But, generally speaking this plot is pretty much what we expect. The next type of plot that I want to talk about, again if you use Excel you’ve probably seen this or PowerPoint, is a pie chart. So we’re going to work with some simple data again let’s say x is 1 to 5, and all I do is call a function called pie, pass in the data right, and so it automatically splits up the data. So the dark blue is the first element, light blue is the second element, green is the third element, and so on. And so there are ways that you can label this to to make your visualization better and so when you show it to people they know what everything stands for, so there’s a lot of documentation on the matlab website for that. The last type of plot that I want to talk about, probably the most useful for data analysis, is called the scatterplot. So we’re going to again return to our sine example, so x = linspace(0,2*pi,1000); So now I’m going to do something a little different, I’m going to set it y = 10*sin(X) + randn(0,1000) and then I’m going to add some random noise with variants 1 centered at 0, and so the reason why I want to scale it by 10 is so that the sine wave is big compared to the noise otherwise the noise will just take over the shape of the sine wave. I did that wrong. Ok, so now if I were to just plot x and y so you can see a noisy sine wave which is ok, if I do it in a scatterplot I can see that pattern pretty much just as well. So the nice thing about our sine X example was that the data was ordered. Now suppose that my data is not ordered so instead of having linspace my X is just a bunch of random points. So X is normally distributed with a standard deviation of 2, and so Y is going to be let’s say 5*sin(X) plus some noise. Ok, so if I plot x and y now I don’t really see anything it just looks like a bunch of craziness, but scattering so a scatterplot that will just plot all the dots so all the individual data points right, and so I can now very clearly see the sine wave that I’m supposed to see. So if you’re doing data analysis and your points are not ordered, you want to use a scatterplot.

Posted on

Learn Matlab Episode #8: Plotting Data

Click here to subscribe for more videos like this!

How do you plot data in MATLAB?

So today we’re going to learn about how to make plots in matlab. So one of the very simplest plots you can make is just to plot a series of data, so one very popular series is the Fibonacci sequence and that’s just adding the two previous numbers to make the current number, right. So here are the first 8 values of Fibonacci, and so to plot in sequence all I need do is call a function called plot and pass in the variable right, and so that’s Fibonacci. Now notice how the X values have been set automatically, so starts at one and increases sequentially. So the next thing we can do to make it a little more complex is to have actual values for X. So suppose I want X to increase by point one every time, so I might do 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7. So now I have an x and y which is what you’re probably more used to when you’re creating a two-dimensional plot. To plot x and y you just pass in x and then y into the plot function, right and so now you can see the y-axis is the same as before but the x-axis is now no longer 1-8 its 0-0.7. Notice that the size of both x and y have to be equal, right so if I do this it is not going to work right. Alright, so the next thing we’re going to do is something a little bit more complex, we’re going to plot a function. So the first thing we need to learn about is a function called linspace. Linspace what that does is it creates the range of values of x for us so we just pass in where we want it to start, where we want it to end, and how many points in between. So I’m saying X is going to go from 0-100 and has 200 points in between. Now to calculate Y all I have to do is sin(x) we’re calling the sine function. In matlab functions generally apply element-wise right so every value of x you call sin(x) and then it assigns the value of that sine X into y. So I’m going to plot x and y again, and so now I see sine X going from zero to a hundred. So one thing you might notice about this is the lines are pretty jagged and it’s not a smooth graph but you know that sine X is actually a smooth graph. So the reason why that is if we look at X again let’s look at the values remember there are 200 points for about a range of 100, and so that’s a very low resolution. The last value here is 100, the second to last values about 99.5, so there’s only about 0.5 distance for every unit of X. So to give us a better granularity we might want to say you might want to reduce the range first of all and then put more points in between. So, let’s recreate X let’s say it’s going to go from 0 to 2 pi and have a hundred points in between, alright, so y is going to be exactly the same when a plot XY again. Alright, and so now you can see that it’s a lot more smooth. So adding to the complexity of this a little bit suppose I want to plot two functions on the same plot and that might be useful if you want to compare two different series, so let’s say the second function we multiplied is cosine of X say y2 = cos(x) and so we type in plot we can see some different function signatures for plot. If you look at the third one it shows us x1 y1…xn yn, so what that’s telling us is that for every pair x and y that we want to plot we just pass the min adjacent to each other. So in our case we’re going to have x and y, and since it’s the same x for both y and y2 we’re going to just pass an x again and say y2 over there. Alright, and so now you can see both sin(x) which is the blue line, and cos(x) which is the green line on the same plot and matlab automatically changes the colors for us so we can differentiate between. One thing you can do if you want to make it a little more fancy is you can specify a line style and so those are also passed in adjacent to the x and y’s, so now it’s even easier to differentiate between the two lines. So the second chart cos(x) is a bunch of dots, and I made a mistake the dashes the default that just makes a regular line. If you put in two dashes it makes them into dashes that you can differentiate between like that. Ok, so now let’s say I want to save this plot to my hard drive or maybe put it on the internet or a document, so you go to file save as. So you see the default file format here is .fig and that is a matlab file so only mad matlab can read those files. There are some other types of formats here that we can look at such as PDF. If you want to save it to a PDF and then print it out later. You can save a PNG or jpg so if you want to put in a word document or you’re writing a paper, or you want to put it on your blog post you might want to use JPEG or PNG, and so I’m going to use .fig since you probably already know what a PNG and JPG look like. Alright, so I already made a test.fig overwrote it so now that I have it, so you can see right here saved into my current working directory. If I want to open an existing figure I do that through a function called openfig, I pass in the filename and it opens back up the figure. And so the nice thing about matlab figures is that I can still zoom in, I wouldn’t be able to do that really with the jpeg would look very blurry, of course i can zoom out, I can use the hand to move around the chart. So, it opens back up the same figurehead before so I can play with it as if I had made it for the first time.

Posted on

Learn Matlab Episode #7: Data Types

Click here to subscribe for more videos like this!

Data types in MATLAB

In this matlab tutorial we’re going to be talking about different kinds of data types, so things that are not what we’ve seen so far such as numbers, scalars, vectors, and matrices. So one thing that we’ve been implicitly looking at are strings. So whenever I display something like this and I put it into single quotes the thing that is inside it is a string. Later on when we look at opening files we will specify the file names as strings. So a string is basically any sequence of characters and so we’ve seen the workspace but there’s also a function called whos that lets us look at all the variables in the workspace and see some stuff about it. So you’ll notice a column called class. So far everything I’ve created is a double so we have no actual strings yet. If I use the class function and I look at an actual string, let’s just use the I found it string again, you’ll see that it returns char. So if I set a variable called my_string = ‘I found it!’ and I type whos you’re going to see now that there’s a variable with the name my string size 1×11. So strings are considered row vectors and it’s of type char. So the next topic we’re going to talk about is structs. We’ve seen so far in matlab numbers, matrices, vectors, arrays, strings, but that doesn’t leave a lot of flexibility compared to other programming languages. So if you’re familiar with Python maybe you’ve seen the dictionary data type, or if you use Java it would be the hashmap, or if you’ve coded in javascript you may have used json. If you’ve ever used any of those before you know that those data types are very flexible because a json can store other jsons, it can store arrays, whereas in matlab we have so far been constricted to only numbers. So in matlab when you want to declare struct it’s very easy so call our struct my_struct.name = ‘My new struct’ Ok, so this creates a struct with one field which is referred to as name and the value is my new struct. If I do class on my struct it tells me that it’s a struct. So the cool thing is you can add new values to the struct dynamically. So I can say my_struct.age = 25, so now my _struct contains two fields name and age and notice that they are different types. So if I do class(my_struct.name) it’s a char, but if i do class(my_struct.age) it’s a double. So there are also some useful functions that act on structs. So is field will tell you if a field exists within a struct, so I just checked if the field name was part of my struct and it returned one because it is. Let’s try gender. So that returns false because gender is not a field in my struct. There’s a method called rmfield. So if I want to remove a field from a struct, I spelled it wrong, so now my struct only contains the field name because I removed the aid field. There’s a function called set field so you might want to use this if for example the name where the value is set dynamically while the program is running, so you can’t do my_struct.something you have to set the field some other way and I spelled field wrong again, so now you can see I’ve added a field called gender with the value F. So the other thing I talked about is that a struct can actually contain a struct. So suppose I want to add a new field to my struct called contact and I want to give a phone number to my contact. So I’ve set the phone number on this struct and so you can see when the struct prints out now the contact field is just a struct but it doesn’t print what’s inside, and so now since contact is a struct I can set other fields as well. Ok, so my struct now contains the contact field but as a struct and the contact is also instruct. So the last thing about structs that I want to mention is an alternative way to initialize a struct. So far we’ve been manually entering each struct value one at a time but you could also do something like this, S = struct(‘name’, ‘Bob’, ’email’, ‘[email protected]’) right and so what that does is it takes in an arbitrarily long list of parameters where the first parameter is a field, the second parameter is a value, third parameter is a field, the fourth parameter is a value, and so on. So the last data type that I want to talk about today is the cell. So the cell is very similar to a struct in that it contains arbitrary fields, the difference is you you might see a cell as more of like an array that can hold indices other than just numbers versus you might see a struct as more of an object that contains information about some some type of thing, and then a cell might be you know like an array of those things. So the cell syntax is very similar to the matlab matrix and array syntax. Ok, so to initialize our cell we’re going to use curly braces we want to set the field to 1, doesn’t have to be an integer, going to set it to the value hello world, ok. So now if I print out my cell it simply contains hello world. Ok, so now i’m going to set another field, I’m just going to call it A and it’s going to be a matrix. Ok, so now I have two keys and two values for my cell. I have the key one in the key A, and then the values hello world and then this two-by-two matrix. So what I can usually do with matlab variables just type it into the command window and see what it prints out. You can see that it has printed out a lot of stuff. So you generally won’t want to do that but notice how now I can access key 1 and it returns hello world, and I can access the key A and it returns the two-by-two matrix that I previously stored. So this is good for when you want to store things by name for example instead of trying to put it into an array, and you can also store heterogeneous information so different types.

Posted on

Learn Matlab Episode #6: For Loops, While Loops, If/Else Statements

Click here to subscribe for more videos like this!

Loops in MATLAB

So in this lecture we’re going to talk about for loops, while loops, and if-else statements in matlab. The first thing I’m going to talk about is the basic syntax of a for loop. So the basic syntax of a for loop is for i= some array of numbers, so it can be anything. So, for argument’s sake i’m going to use just a bunch of random numbers [1 3 5 4 10 7] and then I’m just going to display them to the output. Ok, so all that does is it sets i to the first number for the first iteration, it sets i to the second number for the second iteration, and so on. We’ve seen before that matlab can create ranges with the colon though which is probably a more common use of the for loop, so you want to count from one to a thousand or 1 to 10 and so the way you would do that is for i=1:10 which actually in MATLAB is generating the array 1 2 3 4 5 6 7 8 9 10. So I’m just going to display(i) again and you can see that it goes through every value of i. So now that we have the basic for loop syntax in matlab, what I want to do is go through a little more complicated example. You’ve heard of a concept called the mean squared error. If you haven’t let me show it to you on Google. So the mean squared error is if you’re trying to predict something you’re going to have some error for all the data points between the true values and the predicted values, and so the mean squared error basically takes the difference between your prediction and the actual value squares them all, sums them together, and divides it by the number of points. So you can see how we might be able to use for loops to implement that. So now since we don’t have any data or any error we’re just going to pretend that we do. So I’m going to use the rand and function to create an array of size 1000 by one, these error points are going to be normally distributed which is often an assumption that we make any way with statistical models. Ok, so now I have a 1000 length array of errors which represents the difference so i don’t have to calculate the difference in this equation, and so now we can go ahead and calculate the error. So the first thing we want is the sum of square error, right, so that’s just the sum of all the individual elements. So I can do that by first initializing the sum of squared error to zero, and I’m going to do a for loop from 1 to 1,000 to accumulate the sum of squared error being a square of each individual error. Ok, and so now that I have the sum of squared error I’m going to divide that by 1,000 to give me the mean squared error. So one thing to note about MATLAB is that for loops, while loops and if statements are actually very inefficient, so you don’t want to use them unless you absolutely have to, and so I want to demonstrate how that is the case. So we have this mean squared error example, I’ve pre-written the code, and I use a statement called tick and talk to time how long it takes. So you basically you start your code with tic, you put all your actual code after that, and then you put a talk at the end and then it will
tell you the elapsed time it took to run that code. So I’ve already pre written it so i’m going to paste it out here, tic t-i-c, toc t-o-c, ok. So that took .005668 seconds. Now if we look
again carefully at the mean squared error we notice that we want to square each element and then sum them all together, and so that sounds a lot like the dot product, right. So if you take the dot product of a vector with itself you’re just squaring each element and then summing them altogether. So if I wanted to do the mean squared error equation in a more compact way, what I
might want to do is the dot product between e and itself which is e transpose times e as a matrix and then divided by a thousand, right, and so that gives us the same answer as before, and so if I put a tic and a toc around that I get .002896 seconds. So it’s significantly faster than using a for loop. Ok, so that’s for loops, now we’re going to talk about if statements. So what I want to do is I want to visit I want to revisit the is even problem that we talked about in an earlier tutorial. So if you recall what I did was I created a function that returns one for all the entries of X that are even and return 0 otherwise. So you could do this with if statements let’s say X = 1:10, alright and we want to say Y is initialized to a an array of zeros, and we will do for i=1:10 if mod(x(i),2) == 0 then x is even, right. So, that means we’re going to set Y(i) = 1; else Y(i) = 0. So we don’t really need that else since Y(i) is already 0, but I’m including it so you know what an else looks like. Okay, so now if we look at Y has the ones where we expect it which is where x is even. Ok, so now let’s do a similar example also using for loops and if statements. So the example is the problem is let’s say I want to sum all the elements of an array that are divisible by 3, so let’s just use the same X as before, let’s reinitialize Y to all zeros, sorry we don’t even need to do that, let’s set S = 0; and so now I’m going to loop through all X’s again for i=1:10. So if mod(X(i),3 == 0 so if it’s divisible by 3 we want to sum that element. S = S + X(i); and now this time we’re not going to include the else. So is 18 which is equal to 3 + 6 + 9 which we expect so that’s the correct answer. Now of course there is another way to do this, so if you only want to look at the elements in X so notice how whenever we use i we’re actually saying x(i) we can just do this for x=X right because the little x is going to just go through every element in the big X. We were going to start again so we’re going to reinitialize S = 0, say for x=X,if mod(x,3) ==0, S = S + x; end. So if we look at sum again we still get 18. So now let’s look at another problem. So suppose we want to find an element within a matrix. So suppose again we have the same X and I want to find the number 8, so I can use a while loop for that. So I can say I can set a variable called found = 0 meaning false, and then I can say while not found, so I’m going to initialize an index i = 0, gonna say while ~found, i = i + 1 if X(i) == 8 which is the thing I’m looking for. disp(‘I found it\n’); end Ok, so I made a little mistake there we went out of bounds because i went to 11 and the size of X is only 10, so what we did was we forgot to set found to true after we found x(i) == 8. So I’m going to reset this code, i = i + 1 if X(i) == 8, disp(‘I found it!’); set found to true found = 1; now end the if statement, right. So now we go up to 8, printout i found it, and then we quit the while loop. So while loops only go until the condition after the while is false, so it will continue to go while it’s true, and then when it’s false it will stop. Another way we could do this though is we could again use a for loop. So generally speaking whenever you can use a while loop you can also use a for loop, it’s just the syntax that will change and a little bit of the structure of the code. Suppose I want to look through all of X again, so I want to say if X(i) == 8 disp(‘I found it!’) So now this works but there’s a problem right because in the original while loop we would only go up to 8 and then we would quit, versus this for loop goes from 1 to 10 so that’s inefficient because it’s doing extra steps. So what we can do to avoid that is use the break statement. So just to prove to you that it works i’m going to print out for i=1:10 so again if X(i) == 8 disp(‘I found it!’); break; and so now you see I only goes up to 8. It finds 8, it breaks, and then it doesn’t go to 9 and 10.