for today is going to be about PCB design for RF circuits. Our next presenter is going to be Chris Gamal. Please welcome him with a big round of applause. All right. Thanks for coming today. So let's talk about some goals to start with. I'm just going to talk about how I got into the world of working with RF. I'm going to try not to bury you with math. I know that it's a hot summer day. The last thing you want to do is look at math equations. And then finally, the main thing is to get you some resources as well so you can do what I did and make the mistakes I've made, hopefully a couple fewer. So let's talk about you. Who here has electronics experience? OK, that's a good crowd. How about RF experience? Couple fewer, a couple maybes. Any ham radio operators out there? OK. And then the main thing is you might have some more experience than I do. That's the main thing I was getting at here is that I want to talk about the struggles that I've had as I've moved from DC to RF. That was the genesis for this talk. And show you what I've been doing and show you some of the mistakes I made. And hopefully you make fewer mistakes than I did. So first off, who am I? My name is Chris. I've been doing electronics design for 15 years or so. And I've worked in a bunch of different industries, semiconductor, and industrial, and stuff like that. And I really like the industrial side of things you get to really dig into problems. And you don't have the sixth month cycle that consumer does, although there's a lot of fun problems there too. I also host a show called The Amp Hour, electronics podcast. Been doing that for about nine years now. And I teach people electronics. Obviously teach people the DC side of electronics. I'm not teaching them RF because I'm learning RF. And that's called contextual electronics. So that's an online course where I teach people how to do some of this stuff. And my past, though, I started by working on this. Anyone know what this is? Raise a hand if you know what this is. It's an electronometer. So this is used by material scientists. Basically, you push 1,000 volts into a material. And then you see how much leakage current is coming through. And it can do down to femtoamps. So a really cool thing. But the thing is it's really slow. Like, it's really slow. 10 PLC power line cycles means you're getting about six readings per second. And inside, you're basically integrating everything. And by integrating, I mean you're integrating your measurements over and over again. And things are just going really slow. It's truly a DC measurement equipment. So not quite blazing. Of course, there are microcontrollers in there. There are some higher-speed signal things. But my focus, when I get started in electronics, this is my first real electronics job, was doing this kind of thing. It's so learning a lot of the DC measurement techniques. Over time, it's also morphed into working with sensors and other power supplies. Obviously, everything is a switching regulator these days. But we're not talking about blazing speeds here. We're definitely not talking about RF level frequencies. So what really changed here? And what changed is the world around us. And you've probably all noticed this, too, right? We're all wearing wristbands right now that have Bluetooth on it. It's everywhere. Every product basically throws in a Bluetooth chipset for effectively free, or it has some kind of connectivity method because that's a feature. And that means that every product is going to have RF in it, RFs and everything. Of course, RF is actually everywhere as well, right? We're always surrounded by signals, but every product has it as well, and it's super cheap these days. And so this is a chip I've been talking about lately. This is like a little 400 to 33 megahertz transmitter. And it's 15 cents if you buy 100 of them. I mean, these are levels of cheapness that are like, it's just you could throw it in anything. And so this was starting to motivate me. And I assume that that's why you're sitting here as well, or watching online.  So you're interested in making things. You're interested in this cheap, wonderful hardware. And you're wondering how to actually integrate it. So let me tell you about my story about how I started with my first RF design. So the first thing I did was the most obvious thing, which was to copy the app note. And if you're talking to electronics designers, that's a very common thing. When I started my career, I was always kind of worried about it, like, oh, I'm just copying. I'm not doing anything original. But so much of electronics these days is just re-implementing what's there, because you're in a rush, or you're using what's working. And even if you put everything in exactly the same, there's still going to be problems. And I always say that electronics designers, they make their payday by making things work, even when they're supposed to be working. So I always guess about 80% or so of a circuit design might be, it's a reference design. You're copying a switching regulator. You're using a microcontroller, and you're copying the stuff there as well. And you're just trying to get it to work. And so that's the first thing I did. And there's a great talk as well by Mike Osman, who is here at this conference. He talks about this as well. This is a talk he gave back in 2015 with some rules about how to basically copy RF circuit and go through this. So I kind of think about my talk as an extension of that talk. Some of his rules were using four layers, using integrated components, trying to get everything as 50 ohms, following the manufacturer recommendations that's effectively copying the example circuits, and then routing RF first. I think these are all really good rules here. And I did a lot of these. And so let's talk about my first design. So my first design was a cellular module I was working on for a previous employer and had a little microcontroller, had a SIM card on it, and it worked. So not much of a story there, it just kind of worked. But why did it work? That's kind of the main thing we're trying to get at here. Why did this just work out of the box and with simplicity that I was applying to it? I applied those five rules that Mike was talking about, but the main things to know are the module on board, it was pre-matched with the antenna as well as plug-in. Effectively, the RF section that I was using was connecting the output of the cellular module, modem, to a UFL antenna connector. And then I was using an antenna that was already precaracterized as well. So I had a very, very short distance where I was connecting the output to a port where I was going to plug an antenna into. I basically minimized my interaction, I kept my RF trace short, and I didn't really worry about it past that because this was just a test board, but it worked enough for me to be excited about it. Kind of keep trying and you need those dopamine squirts to kind of keep going. So can we do this all the time, right? Can we always just keep keeping traces really short? Can we just copy the datasheet? Can we follow those five rules? And the answer is, yeah, maybe. I mean, you might be able to. You might be able to just integrate components like that, but it really depends on your design goals. It depends on what you're trying to get out of it. It depends on what you're trying to do with your end product. When you're starting to move into a more uncharacterized zone, say you're making a PCB antenna, you want to make your own PCB antenna where the antenna is built into the board, that's going to be a little bit harder. You're going to need to do a little bit more characterization and testing.  Let's talk about, though, why I wanted to learn more about this in the first place. The first one is I'm a consultant. I do designs for people. And the thing that kept me up at night is I was going to just copy the datasheet, do this thing, but then I'm going to get it to the bench and it doesn't work.  And you open the window and hope that changes something. And you don't know what you're doing. You're basically cargo cult kind of trying to impact your environment without actually having any control over it. And that was a big fear for me. I didn't want to be in that situation. That's doubly true when I'm sitting at a compliance test lab and they're charging $10,000 a day. And I still don't know what I'm doing. So I wanted to basically be able to understand the underlying because that could really impact the ability to get a product out into the world. And then finally is kind of a feel good one as well. I want to make sure, obviously, FCC and CE compliance, these are things that they're testing the same thing that you're not stomping on someone else's bandwidth or you're not operating out of range. But that's also something I don't want to mess up someone else's day because I've got a poorly designed circuit. So a lot of people said that they had RF experience in the crowd, which is great, but I'm going to assume not everybody does. So we're going to go through some of the RF concepts that are kind of tricky and stuff that still kind of trips me up a little bit as well. First off is the frequency domain. So this is a lot of the things we talk about when we're talking about RF and higher frequency things. We start moving from a time domain to a frequency domain. And this is a topic that I think there's a lot of math behind it, but I promise you not to get too much into it. So some of it you're going to have to take on faith. I'm going to show you this is a scope trace. So the top one is a square wave.  What you have there though is you have different frequency components that actually exist within that. And so the bottom part is the actual frequency component of that square wave. Now that doesn't really mean much, and it's really tough when you look at them side by side. They look like they're on the same axis, but they're actually not. So take that a little bit on faith. But I also found a great animation, and we'll hope that this plays. It looks like it's going to. I love this because it's basically showing you the components. Is it playing? Yeah, it's playing. So it's showing you the components that actually exist and make up. They basically sum up and make a square wave. So this is a simplified version. They make up the square wave, and they exist in these different amounts. So the largest sine wave is the main frequency that exists within it, but then there's all these smaller frequencies as well. And you see how that kind of translated to be between the time domain and the frequency domain. We could basically, if we have this information, we can then go and perfectly replicate that same square wave by knowing the frequency information. So this is kind of a tough concept to get your head around, but it is one that will be very, very useful. And I'll talk a little bit more about an instrument that kind of helps you to visualize this as well. I think that went through a full cycle. Another tricky topic is just kind of components. We're trying to squeeze a lot of electronics into a 45-minute talk here. But capacitors, basically, when you're at DC, they're blocking everything. There's no signal that's going to be going through. As you go up in frequency, the signal is going to basically be able to pass through the capacitor. Again, it's not physically passing through, but it's effectively passing through. And this is a really tough concept to get. The main thing to know is that the transition between blocking and not blocking happens because of the value of the capacitor itself, the capacitance of that component. In the inverse way, the inductor allows frequencies to pass through at the low frequency, or DC, and it blocks it at a higher frequency as you move up into the RF world. And so these two things kind of play off on another, and they interact. Again, this is a lot of electronics theory trying to squeeze into a small talk, but hopefully you'll take some of it on faith. Another thing is that in RF, a lot of times we're working in log scales. So you'll see a lot of decibels, or dB, and dBm, which is a more, dB is ratio metric, and dBm is more an actual value. But the idea is here that we're operating in orders of magnitude. And that's really important. You'll see an example a little bit later here. So you'll see a lot of things referred to in dB. And that can, again, be kind of hard to wrap your head around, but it's an important concept because the scale that we're working with of signals, when you transmit a signal and when you receive a signal, it's very, very different in terms of the magnitudes that you have there. As an added bonus, though, you basically, when you're going through a system, you have different gains going through. If you have a 10x gain in something, you have a 20 dB addition to it. So if you're starting at a zero power signal and you go through a 10x stage, you have now 20 dB. You put it through another 10x stage. Now it's 20 dB plus 20 dB. Now you have a 40 dB signal at the output. And so that's kind of nice because it does simplify some of the math once you get past the confusion thing. Wrong way. Where'd it go? And that's the last thing about this is the power is how we're often going to be talking about RF circuits. It's not just current. It's not just voltages. Those obviously make up power together. But we're going to be usually talking about power. And so that's an important thing to know as well. When we're looking at RF subsystems, what we're usually talking about is not degrading the signal. So if we have gain and that's something that we want to maintain over time, we want to make sure that the signal is not being interfered with by the things that we introduced to it. So if we have a connector that has minus 3 dB of gain, that means that we're making the signal worse on the other side of that connector. And that's bad. We want to make sure we're not doing that. Similarly, if we have an output and an input that are not well matched together, and we'll get to that here in a second, that also is going to degrade some of the signal quality as well. So let's talk about what that signal path looks like here. So on the left side here, we have a TX. We have a radio that's transmitting, that's TX. Going through a cable, going through an antenna. And yes, there is a little bit of gain in the antenna as well. But you see in the middle here, there's that red sloping line. That's basically as it's floating through the air and going from a CCC camp cell tower to your crazy camp phone. There's actually some loss in that. And then, again, it hits the antenna on your phone. There's some gain there. And then there's a cable, and then it's at the receiver. All of these things basically say that you're going to be either adding or removing signal by going through these different stages. And the main thing is we want to make sure that we're not adding any degradation or noise as we're going through our systems. Just back to the logarithmic side of things. So again, to give you the scale difference here. So if you have a link budget that's 100 dB, and that means you have a signal. So what you do is you divide that by 20. There's a five there. You take that 10 to the fifth. I know that's math again. But basically, we have a signal that's output it. If we have one watt signal output at a transmitter, that means we would be receiving 1,100,000th of that at the phone in this example that I gave. And so that's kind of why we want to not introduce any other degradation in that, because it's going to make it less likely that we receive that signal at our phone. How do we get through to this? So again, how do we make sure we don't degrade unnecessarily? Well, what we do is we make sure we're matching the impedance of these inputs and outputs so that we don't introduce signal degradation. Now, this is a new concept when I was moving from DC to RF. Now, obviously, it exists at lower frequencies as well. But we want to basically, it's much more critical, I think, at RF, because you are playing with much different impedances as well. Now, one of the baseline things is that you need to have perfectly matched impedances input and output, right? Output might be an amplifier. Your input might be an antenna. You want to make sure those things are as perfectly matched as you can so you're not introducing unnecessary degradation. So let's talk about a counter example. This would be a bad situation that you'd have here. So you have an ESP32. It's a little tiny Wi-Fi module. And then you have a PCB antenna on board. And those two things are not well matched together. When they're not well matched together, what's going to happen? Well, first off, it might not work at all. Or you may perceive it not to work at all. Because you have a Wi-Fi router over here. You have your ESP32 over here. And it's not actually talking to your Wi-Fi router because there's no signal getting from one to the other. So without actually having measurement equipment, you won't actually know what's happening. But to your perception, it's not going to work. Another thing, though, is it may work, but it may not work as far as you want it to, right? So it only works if you're within a foot or within a meter. Sorry. And you expect it to work at 10 meters instead of 1 meter. And then finally, you're going to have a less efficient system as well, right? You're going to have energy that gets either radiated locally on your board and not radiated towards your Wi-Fi router. Or it's just going to be dissipated as heat. So what is impedance, though, right? So I know I'm jumping around topics a lot here. But what we're trying to do is we're trying to get an output like an amplifier and an input like an antenna. These two things to be kind of talking to one another. And we want them to do that by matching their impedance. What is impedance, though? Impedance is basically how it lets, how passes or blocks electromagnetic energy at various frequencies, right? So if we have a 2.4 gigahertz Wi-Fi thing like the ESP, we want to know that our antenna is accepting that energy at that frequency the same as our amplifier is outputting that energy at 2.4 gigahertz. You may be thinking, well, why do we have to do this at all, though? Shouldn't an antenna just work? Shouldn't all this stuff just work? And the answer is, yeah, it should just work, but that's not how the world works. The world's imperfect, right? You may remember the iPhone. iPhone 4, it was a well-designed phone. I think it was the 4. But it was a well-designed phone.  It was changing the impedance of that antenna. And so the matching was all messed up. And you didn't get the same kind of distance to the tower as you thought you would. This is a similar kind of problem, obviously on a much larger scale. So it's not just a hand. It could also be, is it a more humid day? Is there other stuff in your environment? Is there a tree blocking it? Is there other things that are you in a city? Are you in the woods? All of these things can basically impact your signal path and also, on a much closer scale, how your antenna actually works. So even though you may have an antenna that comes out of the box that says, yes, this is definitely 50 ohms, you are good to go, there's manufacturing variability as well. And you basically want to be able to not only measure what's happening, but also then go and modify it so that they're well matched up with one another. So as any simple example, a pine network is one way to normalize your output and your input. In this case, a 1,000 ohm source, which is on the left here. And then you have your little networks of inductors and capacitors. And then your load, in this case, would be an antenna at 100 ohms. You want to normalize these things at 50 megahertz and a bandwidth of 6 megahertz.  Again, we're skipping math.  And they work better together. It's going to have the maximum output that you can have here. Let's talk about some of the measurement tools that would get to this point and understand this in the first place. I had mentioned this a little bit, but a spectrum analyzer. This was, again, that animation showing going from a time domain to a frequency domain. So a spectrum analyzer just basically measures the frequency domain. So you put in a signal into your spectrum analyzer. You're going to see a chart of the frequencies. And this is really, really useful as you start to look at something like Bluetooth, where it's hopping over all these different frequencies and stuff like that. But really, anything that's outputting its own signal, a phone is going to generate its own signal again, a Wi-Fi chip is going to generate its own signal, you then could take that signal and then look at what it's actually outputting in the spectrum and look at it on a spectrum analyzer. You could also do that what we were looking at earlier. It was a slightly different thing. That was an oscilloscope. So that's showing the time domain and then it has an FFT, which is a measure of the frequency. And it's basically a very tiny software version of what we're looking at here. Another thing we can get is a VNA. So Vector Network Analyzer is something now where you're not just sort of a phone might be outputting a signal on its own, right? It's got a battery, it's pumping out RF energy. But if you don't have a thing that's actually actively outputting energy and you want to measure it, you want to measure the characterization of an antenna by itself, you might want something like a VNA. And this is where these things get really, really useful. So I've pulled in some of these low-cost VNAs here and low-cost is very relative. On the top left there is the mini VNA tiny, it's 400 euro. Top right is pocket VNA tiny, 400 euro. And these are some of the bottom of the line, not the lowest, but pretty low. Then the D-PACE is the bottom left. That's a 2K, HP 8753A, starts at 2K and kind of goes up depending on what you have in there. And you're like, wow, that's really expensive. But so I've been shopping around for all these things. I've been on eBay and I've been looking at all these different things. And I was really shocked to see a VNA that was about 550 euros. I saw it and I was so excited about it. And I was like, wow, this thing looks beautiful. Definitely got a bit on it. And then I realized that I actually was looking at the wrong thing. This was $550,000. So if I could borrow some money from people in the audience, we could definitely go in together on this thing. But the reason I'm putting this up on here is to show kind of the scale difference, right? You can go from a 400 euro dollar thing all the way up to a $560,000 thing. And the thing that you're paying for between the two is the one that the 400 euro thing is gonna work more in the 200 megahertz to one gigahertz range. This is showing at a 67 gigahertz range. It's super, super complicated. All of the things that happen in between, all of the losses that I've already talked about, basically everything has to be perfect in this machine. And I think it's a good example of showing kind of the scale of what has to happen. And what you're really paying for when you buy a VNA is the frequency range, right? It'll scale pretty linearly. As you move up in frequency range, your price is gonna move up as well. So personally, I'm targeting things a little bit lower. I wanna do things that are commercially available, 900 megahertz, 2.4 gigahertz, maybe five gigahertz, but those are the ranges that I wanna play with and maybe you wanna play with as well.  The last thing you might need is Calcut. And what this does is basically it gives you an open, a short, and a known load. And basically you can calibrate the equipment that you have that I showed, right? The VNA, you can go and calibrate that thing. And that's important not only because it gets warm, you're in a warm tent or you're out in the sun or something like that and you're using your VNA, you wanna make sure it's well calibrated at that point. But you also might have cables in between. You might have a test setup where you have a cable from point A to point B and you wanna make sure that that cable is in part of your measurement. And that's something that a Calcut can help with as well. So let's talk about some measurements real quick. Again, I know we're on a summer day, we're talking about measurements, how fun. But I think these are really important. Smith charts are basically, it's basically a collection of measurements in a visualization tool. And so this says things like impedance and reluctance on it. And I'm gonna be honest here, I'm not using Smith charts all that much, but I didn't wanna show it because the VNAs kind of allow you to have these outputs, right? You can go and match things a little bit easier. A lot of the, when I showed the C's and the inductors and capacitors that had different values, you can go and find those by using something like a Smith chart and that's really a powerful thing. VNAs also show you things like S parameters though. So return loss, insertion loss, power transfer and reflected power. These are all things that basically you can go and characterize your systems with. So your antenna, your output, maybe you have a gain stage. Basically you can use a one or two port network analyzer to go and measure these different parameters and characterize your system. And that's really useful for when things don't work. And again, that was one of my big things. Return loss is a very common one. So this is also known as a reflection coefficient. And basically this is something you might do when you might plug in an antenna here, right? So this is showing an antenna, right? And it's showing that it's reflecting power at most frequencies and it's a little unintuitive from my perspective. When it's flat at the top, it's saying that's where it's reflecting most frequencies. But at 2.4 gigahertz, it's actually, there's a bunch of power that's not being reflected back. And it's basically showing that it's tuned for this frequency. This is a way to show that this is a well-tuned antenna and how narrow or wide that dip is, as I was going to show how selective the frequencies are. This also plays into VSWR, which is the voltage standing wave ratio. This is what a lot of ham radio people use as well when they're characterizing antennas and you can use a little bit more broadly. But here's an antenna, or sorry, here's a equation here and you see the gamma that's in that equation, that's that funky looking R thing in the equation at the bottom. That is basically the last thing we were looking at here. Sorry, sorry. That is the return loss and that then plays into VSWR and it's basically helping you figure out how well matched it is. So basically as you get a lower and lower VSWR, you have a better antenna. Okay, so that was kind of the measurement stuff. We talked about some of the basics of RF and now I wanted to kind of transition into like the title of the talk. We needed that build up, all that kind of background information here. But the reason I started thinking about all this in this first place is why does DC end up kind of change as you move towards RF? What are some of these assumptions that I've always had as I was doing specifically layouts and new designs and thinking about how I'm gonna put my different components together? How does it all play together? The first one is that a wire is just a wire. So if I hook up a component over here at A and I put a piece of 24 AWG wire over to B, that the signal's just gonna get there. And that's obviously not true at DC either. These are all very simplified versions, but it's definitely not true as you move up in the RF region, right? Because every wire is actually an inductor, especially as you move up into higher frequencies, right? Inductance comes from the magnetic field that are happening as you pass current through a wire, you start to form these magnetic fields here. And they resist, it's basically the resistance of the magnetic field as you're moving current through it. So as you try and turn off the current, the magnetic field is now gonna push back and start and try and it's gonna spike the voltage effectively.  That's the same kind of idea here. If you look at a lot of inductors, what they look like is coiled wires around some kind of substrate as well. And so the main thing to know is that, yeah, a wire is not just a wire. At DC, it doesn't matter because usually the inductance is low enough that it's not a huge deal. And there's a pretty practical example here that many people, especially getting started in electronics, will understand. As you're using a breadboard, breadboards usually operate fine. Even up to 10 megahertz, they're operating fine. If you're blinking something at 10 megahertz, you're probably not seeing it. But say you're operating a clock at a 18 mega, 328. That works fine at eight megahertz. But if you're trying to pump it many, many more megahertz than that, you're gonna start to have degradation issues. And that's because of the inductance as you're starting to wire from one point to another. Basically, not just the wires that they show here, but then the strips of metal that are underneath the breadboard as well. And that brings us to our next thing, which is in DC, of course I never actually thought this when I was designing stuff in DC, but that PCB construction, the stuff that makes a printed circuit board, the fiberglass and the copper and stuff in there, aren't as important as the components that I put on there. So the resistance of a trace, or the inductance of a trace, isn't as important as the resistor that I put on board there. And that is definitely not true as we move up in frequency. So this is a four layer stack up here. This is showing how you, and it's kind of a side view, so it's kind of hard to see here, but you basically have four layers at the top.  If you haven't been to the hardware hacking village, you can go and see some of the surface mount stuff there. I recommend that. But you have traces on the top side. Inside, you might have a, you should have a ground layer, which is usually the second layer. Then you have signal and power on the third layer. And then you can have more traces on the bottom and also some more ground. So these are kind of the normal stack ups here, but the stack up, the actual dimensions of that stack up, how far one thing is from another, that's when it really starts to play together much more at high frequencies than it does at low frequencies. Again, these characteristics all exist at low frequencies, but you don't have to worry about them as much. Or you care about them in different ways, I suppose. So as you start moving up in frequencies, you start to have some more complexities that kind of get introduced here. So this is pretty much the only math that we have to do here, but this is, and it's in a calculator too, so from online, and the links are all here, but say you have a trace that's four mils wide or 0.1 millimeter. The track is about 50 millimeters long or about two inches, and your height above ground, right? So what we're saying is from the top, top layer, layer one to layer two, I was looking up just a standard stack of a set that's about 0.2 millimeters. And so basically you put all these things in there. That means that track has an impedance, an inductance rather of 63 nanohenrys. And if we take that at 2.4 gigahertz, that's effectively a one kilo ohm resistor, effectively. Again, this is operating at these different frequencies, but the idea here is that that actually is a more significant amount of impedance that you would have normally thought from just having a wire. If a wire was just a wire, that wouldn't be a problem, right? You assume that it's zero ohm in the ideal case. It is definitely not the ideal case though. So first off is very important to note that yes, your ground plane should always be the lower, the right below where you're running your RF signals. That's very, very important. You need to define that as well, and you need to pay attention when you're buying a PCV as well. So this is a stack up from JLC. And this is showing that 0.2 millimeters between the top layer, the top layer copper, and the next layer down. And that's important to know that. Not only that, you also need to know the, I think the calculator I was using had actually assumed the dielectric, but basically you're setting up these two physical things in the real world, and you need to understand how those things play with one another. Again, you needed to care about that at DC, but you really need to care about it as you're moving up in frequency for RF circuits, and not even just RF circuits, as you're doing high speed digital, you're also gonna have to worry about it there with controlled impedance and things like that. Okay, another assumption, not, again, a very simplistic assumption for DC is that a capacitor is there to store charge. And yes, that is true, but the idea here is that a capacitor isn't just a capacitor, right? And so these are smorgasbord of different capacitors up on the top left. The top left is through-hole capacitors, and the top right is surface mount capacitors. The top left, when you have through-hole capacitors, they actually have wires literally attached to them. That is what those leads are. And again, we've already talked about this. Those are inductors, effectively. The same thing happens though at low-frequency. You can never really get rid of that inductor, and what we have below there is the equivalent series inductance, the equivalent series resistance, parallel resistance, and then the actual capacitance itself. This is kind of a bulked model of what a capacitor is. And what happens is, as you start to move up in frequency, you start to see effects that you wouldn't have seen at lower frequency. So at lower frequencies, it just acts like a capacitor, it charges up slowly as you move towards DC. At true DC, a capacitor is just a block in the road, right? In more practical terms, below 10 megahertz, it's just gonna charge up at a different rate, depending on its capacitance. Well, as you move up in higher and higher frequencies, that inductance starts to really matter. And what happens here is that the impedance, right, which is a complex mix of inductance and capacitance, that starts to invert as you move past this corner frequency. And so your capacitor, which you thought was a capacitor, that blocking thing is now turning into an inductor, and that starts to really mess up some of your circuits. Your filter might change, your signal path might change, and you really just need to keep that in mind. Another, this is kind of what inspired a lot of thinking for this talk as well. I was looking at layout for how I would do layout for a really sensitive amplifier. Maybe we want to make sure that it's nice and you have ground planes around it, that's fine, right? That's a lot of RF as well. But we would actually cut out around the different circuit elements, so you wouldn't have currents kind of creeping in. You wouldn't have these different things creeping into the circuit. And I say that with kind of like scare tags, because I mean, yes, that is how it works at DC, but that's not really how it works as you move up in frequency again. So some of these are noise reduction techniques that you might use in analog. You might try and isolate some of your parts of your circuit using physical means, but RF don't care. That's kind of the summary there. So here's an app note from Hank Zumblin, one of my favorite applications engineers from analog devices.  And that is less effective when you have high frequencies there. So you need to basically at low frequencies, the cutout means that the signals and the noise that you might be generating with a switching supply won't get around the cut, but it basically, as you start to move up in frequency, that same thing has other effects. And so that's like, at high speed, it will treat this as a capacitor and quote unquote travel through to the other side, but what it's really doing is it's radiating. And what I'm showing here is an antenna that's actually made by cutting a slot in a plane, which is the same thing I was doing. So you could start to, as you cut slots on planes to isolate things, you could also be creating radiation points, you might be failing EMC, you might be pushing some energy into the RF spectrum that you didn't think you were, and not necessarily the best idea. Another assumption here, current follows the path of least resistance. This is kind of a well-known thing, I suppose, but it really is, is it cares about impedance, it doesn't care about resistance. And so what this is showing that, if you have a meandering path here, it's going on this squirrely trace and it's going through a resistor, it might, it would go, the current would flow back to the source directly, but what actually happens, so at DC what happens is that this is true, this is the path of least resistance, it might be flowing through a ground plane, it might be flowing through a VCC, something similar like that, but as you start to move up in speeds, it starts to travel underneath the signal, underneath the trace itself, and that starts to have other impacts as well. So if you started to go through a region of your board that you weren't supposed to be going through, that could have some impacts. And the main thing to know is that at higher speeds, it takes on complex terms, so the inductance really starts to matter in this case. And so as the inductance matters more, where the signal is going to travel is going to change as well. And this happens because as, so anyone in the audience remember the right hand rule? Anyone? Physics? Yeah, okay, a couple people. So basically this is the right hand rule in action, so as you have current going through a wire, the magnetic field's going to curl around that wire, but the place where the inductance is going to be lowest is where that cancels out. So if the current can flow anywhere in the universe, it's going to want to flow directly underneath that signal because flowing back underneath that signal, it's going to cancel out, I don't have two right hands, unfortunately, it's going to cancel out the magnetic field as it was flowing that direction, and that's going to be the best place. So it's going to flow actually underneath, right underneath that line. Okay, so that was kind of the DC stuff. I had also promised in this talk, how does this all apply to Bluetooth and cellular and Wi-Fi and lore and all the new features that kind of come out here. So I wanted to disambiguate all these things. Basically they're all the same thing. So all of these lessons that I've been learning and hopefully you'll be learning or you've already learned, they all apply because the RF energy, basically as you add antennas and as you add amplifiers and everything else, they're going to be operating at different frequencies, but they're going to have the same fundamental rules applied to them. The things that might change are the length of the antennas or the shape of the antennas or the amount of power that you have to put into the system and things like that, but a lot of them are really just brand names and even more than that, a lot of them are actually software stacks that are on top of radios. That was something that took me a long time as well, right? So Bluetooth and Wi-Fi are both 2.4 gigahertz radios. If you didn't know this, a lot of times your phone is actually using the same radio to do Bluetooth and Wi-Fi. That kind of blew my mind when I learned that. And if not the same radio, then possibly the same antenna. Anyways, I wanted to show just some of the frequency ranges we have here. So again, Bluetooth is running 2.4 gigahertz, Wi-Fi is 2.4 gigahertz and five, Lore and SIG Fox, these are kind of low power, wide area networks, 433, 915, 868, depends on where you are in the world and what you're using. GSM and cellular have these multiple frequencies here, but the idea is I wanted to show there's, you know, there's a bunch of set frequencies that you might be dealing with. A lot of them are, you know, either as GSM goes or cellular goes. A lot of them are spectrum that's been allocated by your local governing body, FCC in the US, and I'm sorry, I don't know the European name here. But the idea is that those are kind of pre-allotted to these different technologies. But then there's the more open ones like 2.4 gigahertz and 915 in the States and ISM band and things like that. And the idea is that you're gonna be operating with all of these different frequencies, but the underlying things are the same. So let's talk about some resources that you might wanna look at in order to do what I did, I guess. Some of these are some books that I enjoyed, Practical Guide to RF and Mixed Signal. This is interesting because it's from the perspective of the people who were doing FPGAs for a long time and now FPGAs have radios going on boards as well, right? And they're moving from an eight layer board to a 16 layer board because they now need to have the interplay of these different layers. And so I'm coming at it usually from the low level DC. They're coming at it from the high speed digital and seeing the difference in RF characteristics and what they have to do to their boards is pretty interesting. Another one is just a generic RF circuit design. I enjoy this one. And then the final is a planar microwave engineering. Thomas Lee, Thomas is just like a famous guy in the industry, he's a microwave professor out at Stanford and it's just a really approachable book. Even though there's a ton of math, there's lots of very funny footnotes that helps as you're crunching through math as well. Oh, sorry, another one is YouTube. I love watching YouTube videos. I think that's kind of the highest bandwidth transfer of information. Alan Wolke is W2AEW. He's got a great channel. That's how I learned to, so I started learning Smithchart things and he's a hand radio operator. Tons of other great information there. And then Shariar who does the signal path. That's how I found out about one of those VNAs. He has a great review of those and then he also looks at the 67 gigahertz, $500,000 once. He of course has to give them back at the end. And then finally, a really accessible way to get started for a lot of this stuff. I think things like Mike Osman's, Grace Cocky Adjut's, HackRF, that's a SDR. And so you just kind of get a feel for what's out in the world. Another one is GNU radio plus RTLS SDR. That's like a $20 thing. And they're actually here somewhere in the camp. I think they're giving a workshop today too. And then there's analog devices, Pluto. These are all existing things.  So a quick thank you to Jeff Kaiser or Mightyome. He helped me create these slides. And Derek Kozell who's actually the GNU radio person did this stuff and thanks for coming. So thanks to Chris for a very interesting talk. We have some time for questions. So if you have a question, there's a microphone right over here. You can step up and ask your question. But we also have a question from the internet apparently. No, no question from the internet. Okay, question here of the microphone. I have a question. Is it better to run RF through inner layers or outer layers? The question was inner layers or outer layers. I think the main thing is as long as it's referenced to ground is the main thing. Inner layers versus outer layers you're gonna have different characteristics because it's basically an outer layer is dealing with the dielectric of the board and then dielectric of air. So it's gonna have different like strip line characteristics. But usually what I've done so far at least, it's been real simple. It's been top layer, like Mike says in his presentation too. You do that first so nothing's in the way. Usually I think of inner layers, you're doing that because a lot of things are in the way. But I wouldn't wanna go through any vias or anything like that. I wanna just get it as close as possible to the output port and getting it there. So your mileage may vary. Yes, another question. Yeah, thanks for the talk. Are you aware of any FPGA like devices that use RF circuits? So the one that I was looking at was the, I think the Zinc has an RF front end now. They're not gonna be cheap though. That's the main thing. So I would recommend that. I mean, I'm a big fan of like the open tool chain stuff that's happened with Ice 40s. I think there's some of that stuff. Go pop one of those 433 megahertz radios on there. Otherwise though, not really sure about anything. At a certain point though the, so that was like a transmitter, right? You're basically pumping data to that. The fact of like an FPGA working with a transmitter is, you know, it's just gonna be pushing data to it. You could do the same with a micro or, you know, you could key it in your one if it could go slow enough, of course. But the main thing is that the transmitter itself is gonna be usually separate from the FPGA. I don't know of any integrated ones, especially not at the lower end where I'm usually looking. Any more questions? Final question? Okay, if not then please give Chris a big round of applause. Thank you, thanks for coming.


In mathematical writings, one should watch out for confusing pronouns. I'm saying things like it, this, that. These are all pronouns that could describe almost anything, right? And honestly, this is a confusion in just general English, right? You gotta make sure that your pronoun is clear by context because if the context is insufficient, you can get confusion and we definitely wanna avoid those confusions. Let me give you an example of such a thing. Take the sentence, since x is a subset of y and the cardinality of x is greater than zero, we see that it is not empty. And so in this sentence, it's like, what is it? What is not empty? Clearly we're describing a set, a set is not empty, but what set? X is not empty because its cardinality is not zero, but it's also true that y is not empty because it contains a non-empty subset. So honestly, the statement that it is not empty could apply to both sets, x and y. And so you should clarify which one are you referring to? So an improvement would be since x is a subset of y and the cardinality of x is greater than zero, we see that y is not empty. So it's not that pronouns are bad, by all means, use pronouns when appropriate, but you should always be cautious when you use a pronoun that if you don't have the appropriate context, the pronoun could lead to misunderstandings, it could lead to confusion, therefore it's best to just avoid confusing pronouns in general instead of using words like it to spell it out. Like in this case, it versus y, it really doesn't make the sentence any longer. Like you look at those periods, they look lined up right there, but one removes all confusion whatsoever. So be in that habit of being more explicit in your language and only use pronouns when the confusion is avoidable. That then brings us to the end of lecture 25. Thanks for watching. If you learned anything from these videos, please like them, share them with friends or colleagues if you think they could benefit from them too, if they wanna watch these things. Subscribe to the channel to see more videos like this in the future. And as always, if you have any questions, feel free to post them in the comments below and I'll be glad to answer them as soon as I can.


All right. Any other questions as far as we get going? Appreciate other questions as you download the professors, because that's awesome. The question on the second homework also. What one was that? I was not quite sure. You have a question on the second homework problem? Yeah. Okay. I want to see a sweet diagram on this one. Oh, that is a sweet diagram. What acceleration of the collar, and if I assume it's in a crack and only to that, would give a 15 degree deflection? Make sure you wrote that down on the end of the sentence. What problem is that? I'll say it right now. That's not going to mess this problem. That's not going to mess this problem. Oh man, that broke me. I'm not going to stick here. That's not. No, that's not mustard. That's the color of those peeps, the marshmallow chickens. Is that your Easter set that you got? Kind of is, yeah. I bought it out for Easter. I've got to admit, the board in the first class was awful pretty. Hey, there's a lot more right there. What problem is this? Oh, there it is, number three. Oh, I see. I see. I see what's, I got it. All right. I didn't understand. All right. So that problem is you have this collar that can ride on a rail, and there's a thing hanging from it, arm of a little mass there, and the arm and the, and there have the same mass, and then this is given some acceleration along there, which of course will cause the arm to swing back at some angle. So the question is, what is the acceleration required to put that at 15 degrees? Any slower, of course, will be less, any greater, it will be more. So what'd you do with it? I tried some things like some new moments about peas, where's pea? Point pea is where it's connected to. Point pea's up there. Okay. I've gotten much farther. I'll sleep in the middle of it. Does the collar have a mass too? The collar. The particle, you can just think of something. You don't have to say so. Okay. Do you guys do this? What do you do with it? I don't start yet. I figure I'd save it until the day before it's good. I tried some of the forces on it and stuff. I'm just like a little confused about how they all went up. So it's some of the forces in the abstraction and some of the moments about point pea. It could be somewhere, but I haven't gotten to the bottom. All right. Well, this is, remember what we were looking at on whatever it was, Monday. This is a translation problem. We're looking at just the kinetics of translation. So what you need to look at, I guess, are a couple things. This will have the same acceleration for all, I guess those parents could have been the same length. All three of those must have the same acceleration. And each of those has a w on weight. And so some of the moments about pea that didn't help in it, what do you do next? Some of the moments about pea equals zero. Equals the difference. I didn't split the ball and the arm up. I used this. It's like the, well, where'd you put it? Yeah, that would also make it too difficult because I want to know where the center of mass is between the sphere at the end and the arm. But I didn't know where it is on the arm itself. So that would probably have helped. Probably have helped. You could do a force of X to Y and put a force like a bar. Some force causing the acceleration? Yeah. The bottom and the middle of the bar? The middle of the bar? Yeah, if you did a free body diagram of each of those, then that would put some force on that piece and some force on that. And it's the component of those that's causing the acceleration. But remember, there was a caution about summing the moments I think we've just gotten to. In fact, we're going to hit it again today in some details. Remember what the deal with that was? Very little access there. Yeah, that if you sum the moments about the center of gravity, then it's ideal. If you sum the moments about somewhere else, we'll do several problems today that highlight that. It's got to be about the moment of inertia, about that same point, which we don't always have, but we may have the center of the moment of inertia about G for regular shapes, but then we have to take into account the parallel axis there. Which if you put that in, you'll get just that with it, with the additional fact that A equals R alpha. Actually, R is the same as D in these cases. So if you sum the moments about P, just said they were zero, you didn't take into account this part because you're summing the moments about somewhere other than the center of gravity. So that one's zero, but that one's not. So try it again, see if that works. They're not due today, right? Yeah, so what are you going to do all weekend? Just eat peeps. We're doing kind of the... Well, we're sort of doing an extension of what we were doing before. Remember, what we're doing now is kinetics of rigid buttons. Translation, translation problem. Now, yeah, the bob hanging down had to rotate to that position, but then when you were to do the problem, it had already gone through that rotation. And so it wasn't really a translation, or it wasn't really a rotation problem anymore. Now we're going to do rotation problems, and then next week we'll put them together and do general motion problem. And that's the type of problem of gears running along a rack or something, those kind of problems. All right, so the idea is that we might have some rigid body here, maybe some kind of physical pendulum. A physical pendulum is a term slightly different than the usual type of pendulum that we looked at in physics class where the string has no mass or negligible mass compared to the bob itself, where in this case we have an object where the entire thing has some mass, and so it has a center of gravity that's somewhere other than at the very end length of the string that makes up the pendulum. So when we've got these type of problems we're looking, this is a pure rotation one, because that's all it's doing is about some point. So we might have then angular velocity and or angular acceleration at any one time, and because of that we need to look at the forces involved in holding it there and or other things that may happen. Because of the rotation at any one second it's best probably to use our normal and tangential coordinate systems. And then we can sum the moments about the pivot point, we can sum the moments about G, whatever it is we want to do as long as we deal with it appropriately and we'll look at both possibilities. In fact, every problem we've got today, I think every problem, has alternate solutions to it so you can do it however you best see fit. So for our problems we've then got kinetics equations we can use as necessary. That of course is two equations because of the two dimensions of it for our two dimensional problems. We can sum the moment about some spot and find the angular acceleration from that or maybe we have the angular acceleration and we want to find the forces that are causing the moments that give us that acceleration. That would be a third equation for us. No, remember the stipulation is that you can do it about any point as long as those two subscripts match. So one possibility is that the point you do it about is the center of gravity. You would have to use a parallel axis there. Yes. If you want to sum the moments about some point other than G and you don't have this then you have to use the parallel axis there which then gives you the alternate form I, G, alpha plus M, A, G where then D is the distance between those two axes. Actually just in a two dimensional problem just the distance between those two points the minimum distance between those two points. So either way that's one more equation and in terms of the kinetics that's all we've got. So after that you have to use the kinematic equation if you need more equations. The kinematics equations are the acceleration type equation that we just had a little bit ago. Oh, sorry. That one goes on the other side. So let's make it with respect to G and label this sum point P. Actually it could be again any point that we've got. I know that would have to be P. We have a relative acceleration equation but the acceleration of point P being the pivot point is zero. So a relative acceleration then becomes what we've had before R alpha in the tangential direction and squared in the normal direction. So those equations are possibility are possible equations and then it need be even the velocity relationship between them and that would be only in the tangential direction because we don't have in rotation motion any velocity at any point other than tangential. That bit there is kind of a reminder. Yeah, these colors are awesome for an Easter. You just get in the mood, aren't you? Explain the last part. Velocity of G is just R w tangential direction only. Yeah, where R is the distance from the pivot out to the center of gravity I guess we could call it R g if you want to and then times the angular motion itself. So for the picture shown I have to pick that is rotating that way so at that instant it's velocity is that and it's related to the angular acceleration. I'm sorry angular velocity in that way. Let's do a problem here. Kind of like that one. Imagine a slender rod pivoted about one end at the instruments in the horizontal position there's a moment applied to it. We'll call that end where it's pinned point A. Moment is 60 Newton meters. Also at that moment it happens to have an angular velocity of 5 radians per second. I think I'm just going to stop talking and just draw. It's so pretty. And the length of it is 3 meters and its mass is 20 kilograms. We want to find two things. The angular acceleration it'll have that instant to that moment as it passes horizontal and the reactions at that pin that's holding it to the wall. The blue arrow is a angular acceleration. No an applied moment. It will cause an angular acceleration. Alright we're going to have three solutions for this. All of you can give the same answer and God you pick which one you like the best and study only that one then. But we'll do all three. Alright, free body diagram. Let's see. Of course it's got some weight there. It's got some A there. Since its rotation we'll use the button there. I'm not going to need that spot. I guess I don't want this drawing but we'll put it here like this. That will be the normal direction. That's the tangential direction. Since it's accelerating in that remember normal tangential coordinate systems are defined at the instant by the motion itself. So we've got that. And then there's some component of the fours over here we need to find. So that's tangential direction that's the normal direction right? And then in terms of the load we also have that applied moment. So that's the free body diagram I think. We've got all the pieces. Anything missing? Any forces missing? No, we've got the reaction and the weight and the moment being applied. Why does the reaction force point that way to the opposite direction? Just because that puts it in the positive normal direction. If we're wrong, when we solve for it we'll get a minus. So we're not too worried. Now it helps in these problems especially with the general problems coming up if you also draw a kinetic diagram which is the motion that will result from those forces being applied. Remember in this class the force is going to sum to zero. So we're going to have some angular acceleration alpha that we're looking for. There will be some acceleration of the center of gravity in a tangential direction acceleration in the normal direction. So that's our kinetic diagram. Times to make sure we've got those because when you have to do that MAGD part it will really help if at times you've got that actually written down because you've got to know what that D is in fact you've got to recognize that it's even there. Sum the forces in what we've got to do in all directions I guess. We've got three unknowns so we're going to need three equations. Let's see, tangential direction forces are unbalanced that will cause an acceleration in the tangential direction. Well we can go ahead and put in a T there. We'll fix it in a second. Alright so what forces do we have in the tangential direction? Positives down for tangential direction because that's the way it's accelerating at the moment. So what have we got? W minus A tangential. We know W I don't actually have it calculated but we know M so we don't consider that an unknown. We have W so then that equals the acceleration which will be M times R times alpha because it's the acceleration of the center of mass where R will take just to be the half distance there. That will be the tangential acceleration of the center of mass. This part right here. Why is it minus A T? Because W's down and I drew A T up and that's why arbitrarily chose well not arbitrarily chosen positive direction. Do you need some more colors? I can go get green. Same thing in the normal direction at the normal acceleration and that's our positive direction there. So just to remind ourselves what do we have? An but it's in the positive direction so it doesn't even minus sign. That's it. Equals M A G which is R A squared. In fact we've got all those pieces knowing because we know all those numbers to bet. Vibrations per second. So we have 25 and 70 sorry? So that's the reaction and that's also what's our positive direction. What weight? What weight? Here? Oh that's the minus sign. So we still have two unknowns A T and alpha so we're going to need another equation that relates those so we'll sum the moments about what do they want to do for my first choice here. We'll do it about G M A it's not strictly about G itself but remember the moment is a floating vector. As long as you don't change the direction its location is the same for anywhere along the rigid body. Alex do you have another hand up? If there's some moments about G that we're given moments about A but I see we're down. G's at the center there but moments moments if you have the moment the moment applies anywhere for the whole piece. It's a floating vector whereas forces are sliding vectors. I can't take the weight and put it anywhere I want without changing the problem but I could draw it pushing instead of pulling and there's no difference in the problem. Vectors can slide along their length but moment vectors can float anywhere you need them not anywhere you need them but anywhere. So these two forces cause no moment because they go through point A so only have W causing any moment WR and that's going to cause the object to accelerate. What's IG? Remember there's five ways to handle moment of inertia. Find what? Independence? Yeah, that's IG for a slender rod is the moment caused by the weight about point we're doing the moment about G so actually we want AT we want ATR about G AT is going that's our chosen positive direction so no, it's a plus so that comes out to be meter second so we've got two unknowns again cause MA is known that's the applied AT we don't know it's a moment arm on AT IG is 15 alpha is unknown so we have two equations now two unknowns do I have some of the other numbers for that equation? Yeah, the weight is 196 times R is two unknowns now we can solve it get the last little pieces we already have that to skip the algebra I'll give it to you AT is 19 newtons and alpha 11.3 radians oh sorry, not 11.3 5.9 nothing too tricky there there's new stuff there's things to pay attention to gotta be very careful with the directions so specify that set them down and stick to it remember that moments are floating vectors so even though that's a moment about A, it's a moment from the entire rigid body close to three solutions three ways to solve the same problem there's one of alternate solutions you got all this? I'll tell you something right now alternate solution and it doesn't matter which which everyone makes you more comfortable we sum the moments about G but we could sum the moments about A let's see what that does for us let's see, we've still got MA there


Thanks, so yeah, I'm actually happy to have so many people here, so thanks for coming. So a couple of words about myself. I've been a Python developer and also an advocate since the early 2000s, currently working for Scooby, which is an e-book subscription service, so pay a fixed price per month in your unlimited reading, as much as you like. And besides that, I'm a consultant and trainer, so I give Scython trainings, also in-house, and I'm working on a couple of open source projects for which you may know me, the most well-known to maybe Alex and Mel, and Scython, Looper and a couple of others, so you'll find them on the page. Okay, so Scython is used a lot in big data applications, so I would like to give you a bit of background about what we're doing at Scooby. So as I said, it's an e-book subscription service, and we actually have a very large catalog, so there's more than 130,000 publisher titles, and it's actual publisher titles, not like self-publishing or something, and that's all books available to everyone, every subscriber, anywhere, anytime, you can just click on a book and read it, and once you have a catalog of that size, it becomes kind of tricky to keep it navigable, so it becomes difficult for people to find the books they want to read, and so that's the problem we're faced with, and the question is, how do I find my next book, right? So there's a lot of stuff you can do, you can have recommendations, editorial recommendations, but they don't really scale, to a large user basis, personal recommendations, you can crowdsource that, and certainly you're trying to automate a lot of stuff, so you can do a similar research between users, between books, data mining, and so that's a major thing that we're currently working on, and so you come and play it, and you can actually help us make our book lovers happy, because we're hiring, so go to that page, find a job, and come and join us, and talk to me in the recruiting session for example. So for the actual topic of the talk, the topic is Python, which is, what is Python? It's a couple of things, first of all, it's an open source project, you can find it on Python org, it's a Python compiler, it's almost a Python compiler, so it translates Python to C plus C API calls, and features static code optimization for you, it's actually more than that, in that it's an extended Python language, which allows you to write fast extension modules for C Python without, you know, resorting down to C and writing C API calls yourself, and it's a very nice way for interfacing Python with C and C plus plus libraries, and over the years, so Python is actually relatively old project, it was originally for it from Pyrex, who of you knows Pyrex? A couple of people, okay. So Pyrex was a project that was begun by Greg Ewing, somewhere around the 2000s, and in 2007, Rod Batchel and I forked Pyrex, which had kind of come to a certain store at the time, and founded the Python project, and since then we had a humongous success and huge rise in our user base, and over the time it's really become a major pillar of the Python ecosystem, especially the scientific Python ecosystem, but it's more general than that, so by now there are tons of Python code out there, and it's certainly, it's kind of a technology never, so as a program language, it really helps people to get their job done. So how do you use Python? This talk is kind of split in two parts, okay, so one is general introduction, I'm going to tell you a bit about the surroundings and stuff, and how it works in the surface, and then I'll show you a couple of examples afterwards. So in order to use Python, what you do is you write Python code, or Python code, as I'll explain later, Python then translates this into C code, so you need a C compiler to build a shared library for C Python, and then you can just import it and work with it there. So as you can see, there's a certain, an additional hurdle compared to writing Python code, importing it done, right, so you need to compile and build stuff in between.  Just a couple of lines of Python code, and when you run it through Python, it'll spit out a file, a C file of about 3,300 lines of code, okay? That sounds huge in comparison, and so in order to get there, it's actually quite simple, we have a script, so you can just say cytonize minus i, built in place, in place, i, cytonize minus i, and you work a file, Python scripts, and then it's just going to translate it, it's going to translate it to C, and then it'll build your extension for you, and then you can, as I said, import it and work with it. So why is that so large? In these 3,000 lines of code, there are lots of portability signs, so lots of stuff that says, if the C Python version you're compiling against is this, then optimize something in a different way than for different C Python versions. Very often that's the case for Python 3 and Python 2 differences, but also for new features that came in in your C Python versions, so your code will actually adapt to the C Python version you're currently building it against, and if it's a new version, it's going to use that, otherwise it will just fall back to some implementation, and it might be slower, but it's still going to work. The next thing we do is we copy all the original source code into C code comments, so you can actually, when you read the C file, you can actually find your entire source code in there, which makes it easier to figure out what's happening when, and which source code line contributed to what, you know, huge amount of C code, and in any case, it's definitely, it's definitely a lot of code that you really don't want to write yourself, and for which you want to use code generator. So we write C, so you don't have to, okay? Building the whole thing, I already showed this Cyton iscript, which is really nice, but it's kind of the thing you would use for local development, and when you want to distribute your code, you'd use distutils, and you'd let distutils build the extension module for you so that you want to install your package, have no problems in building it locally and, you know, getting it all set up. We also have a helper for that, so basically what you do is you would import the CytonS function, after which the CytonS script is modeled, and then you would just say, Cytonize some source files, and it will build the extension, so the metadata, the extension metadata for you, and it will compile that for you, okay? For the more involved cases, where you want to build against external libraries, for example, so you have, you know, linker set up and maybe include half and stuff like that, you can also use this, the standard distutils way of setting up the build metadata through the extension class, you can configure it, just look it up in the distutils config, configure it yourself and then pass that into CytonS, okay? Then it's just going to do the right thing for you, okay? So, I already mentioned that the code CytonGenerate is very portable, actually, so it generates C code that compiles with all major CNC platform compilers, on all major platforms, so Linux, Mac, Windows, and some others, well, less often tested, but in general work. And in all Cyton versions from 2.6 through 3.5, which is currently not released, but, you know, it's on the way. Partly also in PyPy, so PyPy is a port for the C API, and we have a lot of special cases in the CytonGenerate C code, which say, if compiling for PyPy, then do this, otherwise do something else, which help in, A, making it work at all in PyPy, and sometimes making it less inefficient. So, the language syntax of Cyton itself follows Python 2.7, with lots of feature back ports, whatever comes in from Python 3. We just adopt it as long as it doesn't conflict with any Python 2 features. And if you want to have actual Python 3 syntax, you can use the usual future imports, from future import, absolute imports, print function, this kind of stuff. Or you can just say leave a compiler directive at the top of your source file, which says language level 3, and then you get Python 3 semantics for your file. For language features, Python language features. So, one of the tests we are on in our continuous integration test is the C Python regression test suite of Python 2.7 and 3.5, and we're at about 98% success, so it's pretty close to full Python language coverage. What's definitely in there is everything you would normally know, like classes, control structures, loops, tri-finally, so exception handling, comprehensions, we have support for async and await, which will be new in Python 3.5, star unpacking, same thing, so this extended star unpacking, which is new in Python 3.5. So that will all be in the next Cyton release, which is currently in beta, so we have published a beta release yesterday. So the first beta is out for version 23 of Cyton, and we have support for profiling, tracing, coverage analysis, like lots of tools that help you understand, analyze your code, and make it better. Okay, a couple of deviations. We don't have frames inside of functions. Who here knows what frames are? Okay, that's definitely a clear minority. So if you never came in touch with frames, then this one usually hurt you. You'll notice frames in a couple of places, and that's tracebacks, because tracebacks are actually built on frames, but we support those, okay, so you'll get a nice traceback from your Python code if there's an exception being raised. That's all supported. And then obviously, yeah, there are bugs in every piece of software, and you can find them in the backtrack there. As for speed, so Cyton generates actually very efficiency code. There are many static and optimistic optimizations that it applies to your code. It generates optimized code for standard types and for many built-ins that you would use, and has static type inference within functions, okay? Okay, so it kind of can decide what you do and when, and it understands your code in a way that's... So it has better static knowledge about your code than the normal CPython interpreter would. One way we measure speed is with the Python benchmark suite. There's a bunch of software packages that were assembled to measure the speed of the performance of Python programs. It's actually a real-world code, including stuff like Django, some template engines, what else is in there? Some computational little benchmarks on very different... So lots of different stuff that you can run to see how fast your interpreter is. And we run that in Cyton, and the problem with that is that the applications that have been tested there, they are a bit outdated. Many of them were written in a pre-Python 2.6 area, and that means they don't use many of the more recent Python features. So it's a nice benchmark, but it's not very telling for any recent application that you would write. So in that benchmark suite, we're currently about 1.3 to three times, depending on the benchmark, faster than Python 3.5, and hand tuning those benchmarks a little to make Cyton generate better code for them, gives you something like four times to 50 times. And how do you do that? Well, static type declarations. So Cyton is actually a language that supports optional static typing in addition to being Python. And so in your code, you can say... You can declare variables, for example, having a certain type, and that is an extended syntax, which says cdef some type x for variable declaration, so it's extensions to Python language, which then are only understood by the Cyton compiler, not by Python. What does that do? Well, once Cyton sees these declarations, it can drop your code from object operations into plain C code. So when it knows that a variable has a C integer type, for example, it can say, okay, adding a value to the variable can be done in C, don't need to wrap any object, so that's faster. And the nice thing is that it's optional typing, so you can employ these type declarations exactly where performance matters, and that's the way we made these benchmarks way faster than what you'd get from simply compiling the Python code. Especially from computational loops, usually the speedup you get is somewhere between 100,000 times. Okay, the actual topic of today's talk is talking to native code. So I won't talk much about speedups, I'll talk more about how do you connect to external libraries, how do you talk to them, how do you see from Python? So since Cyton code translates to C code, talking to native code, external code, external code written in C or C++ is actually completely natural, it's totally straightforward, and so that makes Cyton a foreign function in the face, but the cruel way. You can think of it as Python with C data types, and the nice thing that the compiler gives you is that these types converge back and forth from Python objects automatically for you. Cyton understands pretty much all C data types, and most of C++ own data types, and it includes C functions, pointers, structs, arrays, and so on, and C++ classes, templates, operator overloading, and so on and so forth. Okay, and that's the end of the first part, and here's the demo. First of all, C features. Who does not know the iPython notebook or the Jupyter notebook? Okay, everyone does, that's cool. So in case there's someone here who's been working on it, so who's been contributing to Codebase, thank you everyone, it's a great tool, it's wonderful. And has Python support, so all you really have to do is you open a Python notebook, and then you say, load xcython, which enables Python support. And currently, as you can see, I'm using Python 3.5 and Python 23.1, so use release. And so here's an example. This is what a Python code looks like. So I start my cell here with double-percent-cython, which tells the Jupyter notebook that here's the Python cell, and please compile this for me and then run it. And I can compile it, and just works. And what I did here is I defined one little Python function. I defined a Python function with a typed argument, so the argument that's been accepted must be a C int. That's what I declared here. Then I have one function here, which is a C def function, which translates into a plain C function. And then the last thing I have here is a Cp def function, which is kind of a C function with a Python wrapper. So the Python wrapper is automatically generated for you. And now I can call my Python function, which is what I want. I can call the typed Python function, which does the same thing, with a difference that if I pass in something else, then it ints. Maybe C, for example, then I get a type error. And I get it on entry, so it's automatically checked for me when calling the function. Okay, then I can try to call the C function, and I'll see if there's a name error. So the C function doesn't actually appear at the module interface, because it's a C function, right? It's not a Python function. But what I can call is the Cp def function, which then internally calls the C function, and that's, as I said, a C function with a wrapper around it, so the wrapper becomes visible through Python code. Okay, a bit of function jit-compiling. We have a decorator called siphon-compile. So this is not a siphon cell, it's a Python cell, and you can import the siphon module and decorate a Python function with a decorator, and then run that, and when you call the function, it executes, and the result is down here, it executes the compiled function for you, and you can see that because Cog is not just a Python function, it's a runtime-compiled function, okay? So that's how you can do runtime compilation from within Python. Okay, calling C functions. As I said, it's entirely natural for siphon code to talk to C libraries, and the way this works is, well, we have a standard, a couple of standard declarations that we ship that includes a large part of libc, libc++, the C Python C API, so lots of C functions that you can simply import use, done. So the way it works in siphon is you say from libc, so the libc library, there's a module in there called math, so the math header, from libc, c import math, and then you have access to the libc math functions, and you can say math sign of pi by two, run that, and it'll give you one, okay? One nice thing you can see here is I didn't just say, siphon, I said, siphon minus a, and minus a means annotate, and what I get out of it is an annotated HTML version of my source code, and that allows me to see what siphon made of my source code, okay, because now here you can see it's exactly the source code as you saw above, but when I click on the line with a plus here in front, it'll show me the C code that generated for it, okay? So I can see, so what it does here is it does the computation pi by two, calls the C sign function on it, then calls the C API function on it, which converts the double C double results to a Python float object, and then it calls a helper function which prints the result, because that's what it says here, prints the result of sign, okay? The reason why it's a bit yellow compared to the other lines is that there's interaction with Python going on, so there's actual Python object operations here. As you can see, the result is calculated in C, but then afterwards I create a Python object from it and call the print function, and that's everything that adds up to the darkness of the yellow line here, so it's pretty dark, meaning quite an intensive Python operation going on, okay? Memory allocation, same thing. We have the standard lib headers, readily declared for you, so you can just say from libc standard libc import method and free, and then you can call method, have to check the results, obviously, if it's now raised a memory error, if it is, in other words, in this case, we just free it, okay? Works for me, okay? What you should do though, especially for little amounts of memory that you need, is use the C Python heat memory management, because that's pre-adocated memory, so it doesn't have to talk to the operating system, and it's faster for small memory areas. So what I'm doing here is I replaced Melloc by PyMelloc, same thing, check the results, and here I'm assigning some values to the memory buffer that are allocated, and then do a little computation, print the results, and free the memory, okay? Important here, try finally, because even if something goes wrong here, I want to be sure that I free my memory. Up, execute it, it gives me four, so it's adding up one and three, okay? Okay, one nice little feature that came in in 0.22, I think, so last release, Python version, is automatic wrapping of C functions. So here I'm C importing the math and standard lip modules from lip C, and take the sine and A to I function, that's actual C functions, and assign them to Python variables. Okay, which makes them objects. So I can execute that, and you can look up what it does. So here it actually calls a helper function that it generated for me, which takes the C sine function and wraps it in a Python callable for me, okay? And then it just adds it to the global module dictionary so that it becomes available to Python code. And then I can just call it from Python code, okay? So pi sine of half is apparently this, and I can take the A to I function, pass in the byte string, and it's one, two, three, okay, as a number. Okay, it's actually as simple as that. Okay, external libraries, here's a bit more often involved example, because what, so talking to lip C is nice, but it doesn't usually get you very far, okay? So there are lots of helper functions in lip C, but what you normally do is, so you want to talk to your own code, okay? So you have some C code lying around, or an external library that you want to wrap that you want to make available for Python, use it from your Python code, and so this is how this works. So I chose Lua, because I've also written a Lua wrapper for Python, which is called Luka. And so I happily copied some code from it for this presentation. So there's a Lua header file, and I copied some declarations from it, which is a state, so that holds the runtime state of the Lua runtime. I can create a new Lua runtime, and there's a function for closing the Lua runtime, so cleaning up, and I can load a code into the runtime executed, and then so Lua is a stack machine, and so I have some function to operate with a stack, and to call a function on it, and to stuff it. So that's all the declarations that are needed. Important bit here is it's not complete, so the Lua header file is definitely way larger than what I've copied here, but that's all I needed, okay, and that's all that Python needs to know. And once that's declared, I can actually just use it, okay? So here's a function, run Lua, Python function, and I pass some Lua code in there, and then first thing I do, I make sure it's a byte string, so I convert it to a gf8 of its unicode, create a new Lua runtime, raise an error if that fails, load the code into Lua, raise a syntax error if that fails because that's activated in the Lua parser, and then get a result from that, call it, execute the Lua code, if that fails, I just raise the runtime error, saying your code was broken, please try again. And then here's a very simple return value conversion, so I'm expecting a Lua number back, raise an exception if it happens to be not a number, otherwise I just convert it, so there's a helper function for converting the Lua number back to a C number, and then I return it. Okay, so that's all I need to execute Lua code in Python, or in Python in this case, to make it available for calls from Python. Again, try finally, so there's a bit of cleanup. After creating the runtime, the Lua runtime, I need to make sure that I probably clean up afterwards, regardless of what happens, so any exceptions shouldn't, you know, leave the memory leak or something, or leave the runtime in memory, so I'll just clean everything up afterwards, and then I'm done. Okay, so one thing I'm doing here, now I take some Lua code and pass it into the function, and at the end I just say, so I create a function, Lua function, and then I call it in the end and return the result. Did I execute that? I didn't, okay, I'm executing that, I'm executing that, and it gives me the 10th Fibonacci number apparently, so it's a recursive version of Fibonacci, and I can benchmark that, that's gonna give me about five milliseconds, and the thing I didn't mention is, why does this actually work, right? So I'm using Lua code, but I'm not linking against it, yes I am, so what I'm saying about here is, when this compiles in this details, and I'm creating my shared library for Python, please use this include here to find the header file, use these libraries to link against, okay, so I'm specifying that directly in my module, and now I linked against Lua 5.1, I know if I replace that by Lua.jit, which I also have installed, all I have to do is replace the include directory, so for fun I'm just gonna do that directly in my source code, I could also do that in my setup file, but since I have an iPad and notebook, I'm just gonna do it in place, compile again, run the code again, same result, and it should be a bit faster now, yeah, it's about twice as fast, okay, just replacing the Lua implementation. Okay, what else do we have? A little bit of nice syntax in C arrays, so C arrays are actually quite clumsy in C because they're basically just pointers, and sometimes C knows the length but doesn't really care about them, so Python knows the length of a C array, and it has better ways to deal with them, so what I'm doing here is I'm creating two C arrays of integers length 10, A and B, and then I'm assigning to a slice, so I'm saying I have five values on the right, and I'm assigning that to a partial slice of the C array, then one nice feature is I can assign by value, so C doesn't support that, when you assign an array to another array variable, it's just gonna assign the pointers, or it's actually gonna give you an error and say you can't assign arrays, so Python does a array copying for you, it's assigned by value, very nice feature, and then I'm just assigning to the other half of the array a couple of more numbers, I can just iterate over the array by slicing it, I could iterate over the whole array by just saying four I and B, here I'm just interested in the first three numbers, so I'm iterating over those, printing those, I just want in this case, and then I'm returning B, so what you see here, so I created a function for it, okay, so that's all I do, and you see the iteration actually uses a C loop over the array, then we have here the assignment, which just translates to a mem copy, pretty straightforward code, the partial assignment here translates to a temporary array and a mem copy, so this is all stuff that Python does behind your back and it just does it right, and then when I call my function, you see the iteration here, two, three, four, so iterating over the first three values, and then when I return in the array, it automatically translates to Python this and I get the mapped Python object as a return result, okay? Okay, I'm going to split that and I'm just going to show you a bit of C++, because you may not like C++ and there are lots of people who don't like C++, but it's actually really nice when used from Python, and that's the only way I would ever use it, okay? So same thing here, load like Python, same Python version I'm using, and now first I have to tell Python that this is supposed to be compiled in C++ mode, which enables all sorts of C++ features, meaning operator overloading, meaning class support, templates, and so on, and as I said, we ship declarations with Cython that includes libc++, including, for example, what I'm using here in the vector class, so I can just say from libccp, ccp vector, c import the vector class, and so here I'm defining the Python function with a stack-allocated C++ vector object, and then I can just insert a value, there's a pushback method, whoever came up with that name, on a C++ vector class, and then return it. What do you think that returns? Someone saying stack fault down there? Nope, because that's not what it does. It returns a list, okay? So that's a say for mapping from a C++ vector to Python this, so whenever you make Cython convert a C++ vector or a C++ list for you, any container knows, it'll just convert you to a list for you, or to a C++ map to Python dates, and so on. So you get the obvious representation in Python as a default, okay? Another little example, a vector, so the nice thing here is that Cython does the automatic memory management for you, so this is a stack-allocated C++ vector, meaning it goes out of scope and dies, okay? All handled. You can do the same thing with an extension type, so this is the way Cython defines extension types. Anyone implemented an extension type with a C API? Okay, a couple of people. What you do basically is you take an existing extension type, copy over all that it has, because there's a model C code, it's all struct definitions and stuff, and then you adapt it for your needs, because you're never going to infer them the way this works from the documentation, okay, so you just copy it over, adapt it. In Cython, you don't need to do that, okay? You just say, see Dev class, and it's done, okay? You have an extension type. Here, I'm adding a field, so an attribute to the extension type, to the object in this case, which is a C++ vector, and that makes, and that ties the memory management for the C++ type to the lifetime of the Python object. So when the object is created, it creates a C++ vector for you, and when the object dies, it will clean up for you, okay? Okay, so there's an add method, which is called pushback and representation, and so I can execute that, and then the wrapper is actually just implemented as wrapper of values of the C++ vector, and when I execute this, as you can see, the representation of the class becomes a list, because that's the representation of the C++ vector converted to Python object, okay? So it just uses wrapper of list, and that's what you get. Okay, I'll just leave it at that, and continue that over here. It's a conclusion. Cython is a tool for translated Python codes to efficiency, and it's very easy to interface with external native code, as you've seen, you just write the code, and it just works. You can use it to speed up existing Python modules by concentrating on the optimization, rather than a complete rewrite, which you would have to do, write it in C, avoid writing C, just do it in Cython. It allows you to write C extensions for C Python in actual Python syntax, in using Python semantics, using Python language features, so it allows you to write C code without actually writing any C code at all, and you can use it to wrap C++ or C or C++ libraries in Python syntax just by using objects, doesn't matter if it's Python object or C++ object, it looks all the same, it feels the same, okay? Thanks. Thank you, Stefan. We have time for one question, on my veto, short questions. Are there any questions? Down here, yeah, okay, anyway. Oh, I was wondering when calling CRC++ code from Cython, what is the developers of support for the new standards of C and C++? For what, for the new standards of C++? C++ 11, so on and so forth. It depends on your C++ compiler, so Cython doesn't really care. I mean, it can be C++ 11, can be C++ 19 or whatever there is, so it just depends on your C++ compiler. So the question was, does it generate C code or C++ code? It depends, so it generates C code for almost everything, except when you interact with C++ objects, in which case you get C++ code for it, okay? So it knows about operators, for example, if the operator plus will translate into HB, so you get C++ for code for everything that's C++ usage in your Python code. Okay, thank you. Thanks. Once again.