Ambulatory Glucose Profile – MATLAB tutorial

This post is meant to give a very short demonstration on how to generate a AGP-plot from measured CGM data. AGP stands for ambulatory glucose profile and is basically a visual condensed representation of multiple days of continuous glucose measurements. It allows to visually examine time ranges that are problematic and require adoption of the therapy. It does so by showing how much the glucose levels fluctuate over the course of a day. Therefore, data of multiple days is used to generate inter quartile range plots. Since I did not find too much information about how to generate these plots, i thought a quick and dirty tutorial might be helpful. For more information look here.

So let’s start!

At this point, we assume that the CGM data has already been preprocessed in a ways that each day is reduced to 24 hourly average values (x = hour, y=glucose value). Given data of 14 consecutive days (assuming no missing values), we have a total of 14*24 = 288 glucose readings.  The sample code below creates a random dataset with  a peak in placed around 6 AM (hyperglycemia in the morning) and a minimum at around 6 PM (hypoglycemia in the late afternoon).

x = 0:1:24';
y = 100+100*rand(14,25).*repmat(sin(x*2*pi/25)+0.7,14,1);
h = plot(x,y); hold on;
xlabel('hour of day');
ylabel('glucose concentration in mg/dL');

We can plot the mean glucose profile by calculusing the average for each day hour of all fourteen days. To do so, we use the mean function of MATLAB:

havg = plot(x,mean(y),'r','lineWidth',3);

Now, the 25/75%-IQR inter quartile ranges shall be added to the graph. This range covers 50% of the most common glucose values and gives a good indicator about how much the glucose values fluctuate. To show the upper and lower bounds, we have to calculate the 25 and 75 percentiles for each hour of the day.

p25 = prctile(y,25);
p75 = prctile(y,75);
h25 = plot(x,p25,'b--','lineWidth',3);
h75 = plot(x,p75,'b:','lineWidth',3);
legend([havg,h25,h75],'mean','25 percentile','75 percentile');

We want to fill the area between the lower and upper percentiles to better visualize the range. We can do this using Matlab’s fill function. The function takes a polygon in the form of x,y-coordinates and fills the area using a solid color. From both percentile curves, we create a polygon by simply appending the lower curve in reverse order to the upper curve. The x values must therefore also be reversed using the fliplr function.

h2 = fill([x,fliplr(x)],[p75,fliplr(p25)],'b','');

The edgy appearance is due to the small number of intermediate points (we only have 24 values per curve). If we would generate more support points like for every minute of the day or so, the problem would not be so dramatic and eventually could be ignored. Here, the shape looks very unsatisfying and should be visually improved. Therefore, we create a smooth transition between the intermediate points using spline interpolation utilizing MATLAB’s interp1 function. It takes the original x and y values at first and then the new x positions for which the interpolated values shall be generated. The last argument indicates the method used for generating intermediate points.
Thus, we first have to create the interpolated curves using 200 intermediate points which is enough to create a smooth appearance. Then we repeat the above procedure to fill the area between them. The figure below shows the smoothed results.

xInterp = linspace(0,24,100);
p25Interp = smooth(x, p25, xInterp);
p75Interp = smooth(x, p75, xInterp);
h2 = fill([xInterp, fliplr(xInterp)], [p25Interp, fliplr(p75Interp)],'b');

We can add another inter quartile range. Traditionally, this would be the 10/90%-IQR.

p10 = prctile(y,10);
p90 = prctile(y,90);
p10Interp = interp1(x,p10,xOS,'spline');
p90Interp = interp1(x,p90,xOS,'spline');
h3 = fill([xOS,fliplr(xOS)],[p90OS,fliplr(p10OS)],[0,0,1]);

The average value should also be smoothed and added to the plot at the very end so that it is not covered by the filled polygons. Note that we used a smaller alpha value for the second IQR to distinguish between them. The overall result and code is shown below.


%% create random sample data: hourly average glucose values for 14 days
x = 0:1:24';
y = 100+100*rand(14,25).*repmat(sin(x*2*pi/25)+0.7,14,1);

%draw data and average curve
h = plot(x,y); hold on;
xlabel('hour of day');
ylabel('glucose concentration in mg/dL');
havg = plot(x,mean(y),'r','lineWidth',3);

%% draw percentile curves
p25 = prctile(y,25);
p75 = prctile(y,75);
h25 = plot(x,p25,'b--','lineWidth',3);
h75 = plot(x,p75,'b:','lineWidth',3);
legend([havg,h25,h75],'mean','25 percentile','75 percentile');

%% fill 25/75 IQR
hIQR = fill([x,fliplr(x)],[p75,fliplr(p25)],[0,0,1]);

%% fill smoothed version of 25/75 IQR
%first, we remove the old plot
%smooth using spline interpolation
xInterp = linspace(0,24,200);
p25Interp = interp1(x,p25,xInterp,'spline');
p75Interp = interp1(x,p75,xInterp,'spline');
hIQR = fill([xInterp,fliplr(xInterp)],[p75Interp,fliplr(p25Interp)],[0,0,1]);

%% add a smoothed 10/90-IQR
p10 = prctile(y,10);
p90 = prctile(y,90);
p10Interp = interp1(x,p10,xInterp,'spline');
p90Interp = interp1(x,p90,xInterp,'spline');
hIQR2 = fill([xInterp,fliplr(xInterp)],[p90Interp,fliplr(p10Interp)],[0,0,1]);

%% add smoothed mean curve
yavgInterp = interp1(x,mean(y),xInterp,'spline');
havg = plot(xInterp,yavgInterp,'r','lineWidth',2);

DIY, Electronics

DIY concrete lamp with switch and power outlet

Today I finished my new project, a concrete lamp with power outlet and switch for the lamp. Usually, you will only find the one or the other. Actually I’ve built a concrete lamp before butwithout the switch and a power outlet.

To build the lamp, i followed the instrocutions of this youtube video. I used 5 pieces to build a 13cm*13cm*13cm cube. I mounted two flush boxes to hold the power outlet and the switch.

If you don’t know what you’re doing, don’t do it! Attention: Concrete might actually be conductive!

Mounting the boxes was actually more difficult than expected because the boxes overlapped by half a centimeter so I had to trim of a bit. I drilled holes for the screws to hold the boxes in place. I also used some quick concrete to seal the gaps so that no concrete could enter the boxes.

After the concrete dried (like 4 days), I carefully drilled the holes to mount the outlet and switch. Make sure your switch is a two-pole switch! Otherwise your lamp socket would eventually still be connected to phase depending on which way you plug in the power cord! Afterwards I applied two coats of concrete sealer.

Finally, I only had to connect the plug to the wire. I used white again to match the overall design which turned out to be a good choice I think. I used white heat shrink tubing as protection of the fabric cord. And finally …tada: done! Feel free to copy and comment!


App development

UIViewController offsets scrollview subviews

I was just wondering why my Scrollview Content would always beoffset to what i set it up in Storyboard (Xcode). I use a UIViewController and changed it’s view’s class to UIScrollView.

The layout of my scrollview in Xcode Storyboard
The layout of my scrollview in Xcode Storyboard

The problem was, that all the content which i played out in Xcode was offset by the height of the toolbar during runtime. This did not happen when the view was of class UIView, as by default. I therefore tried to set the subviews frames during runtime but this seems to be restricted by the loaded nibs somehow.

Subviews have a offset in y direction with a height of the toolbar
Subviews have a offset in y direction with a height of the toolbar

for(UIView* subview in self.view.subviews){ [subview setFrame:CGRectOffset(subview.frame, 0, -[self navigationController].toolbar.frame.size.height)];}

I finally found the reason for my problem in the attributes inspector of my ViewController.

Turn "Adjust Scrollview Insets off"
Turn “Adjust Scrollview Insets off”

The Option “Adjust Scroll View Insets” must be disabled for this purpose. It’s probably something which tries to prevent the offset from being hidden by the toolbar. But using the layout in interface builder to manage your subviews obviously interferes with this option. You should therefore turn it off!

offset removed by turning off "Adjust Scrollview Insets"
offset removed by turning off “Adjust Scrollview Insets”




HSV RGB IKEA lamp + IR Remote

My first Awesome IKEA lamp had multiple trimmers and push buttons to setup speed, brightness and color. I now added a remote control to get rid of all these. I chose one of these common rgb remote controls that are shipped with every other cheap rgb lamp. I got it from The led i use is a 3W rgb star led from ebay with the following specifications:

Red=2,4V, Green=3,4V, Blue=3,4V, 350mA per channel

Make sure you do not get one with common “+”; if so you can’t use p mosfets but must use the n version instead. Common ground is no problem. You’ll get a complete partlist from the eagle file. The main changes from the original layout are:

  • IR_REMOTE: CR2025


I uploaded everything including the schematic, board and the code: Download!

The code is neither commented nor nice. Feel free to improve it 😉 There is a little problem with this version; the p-mosfets seem to have great leakage current so the leds wont turn off completely. I think the PORTD has to little output power…


Awesome HSV RGB IKEA lamp

When my girlfriend gave me her birthday present, i was overwhelmed. The drawing was perfect, I loved the motive, details and shadows. And it came that she had her own birthday! This was the moment i decided not to buy one of her wishlist’s entrys (yes i keep track) but build something by my own too (let’s just stick to buying things okay?). I thought about painting a picture too but decided against it. I just didn’t want to reveal my inner artist and make her jealous.

Since im a student of it and electronics, i chose to build something wired.  It had to be cool but also instructive for me i though. I always planned on building a “proper” RedGreenBlue LED Lamp which could fade between colors. But i never found the time nor the motivation to do something símilar in my freetime. The first attempts on my Arduino Board were pretty basic. The colors were fading, yes, but randomly which means (in RGB color space) pretty white changes to a white reddish to bright white reddish to a bright white reddish with a touch of green or blue. This wasn’t  that exciting at all. I “went” to the community and asked about color spaces. My idea was to make a distinction between color and brightness and avoid same colors with just different portions of white. I found that the hsv (hue saturation value) color space met my needs. It allowed me to use a simple potentiometer for choosing the color (hue) and a further one for the brightness, while the saturation remains constant (no white parts please ;-))

I started coding (checkout github) the code for an atmega8 controller and tested it on a breadboard first. I found and bought a perfect fitting aluminium globe at IKEA which could hold both the led and the electronic circuit. Then I began with the final board layout in Eagle and ordered the other parts at reichelt: Three Potentiometers (two logarithmic ones), three nmos transistors, resistors, capacitors, push buttons, toggle and finally the power supply (5V2A).

I attached the parts to the new board and tested it with Georg who provided an oscillocope and even a 3D printer. I  designed and printed two plastic parts for the globe to hold the led and the board.

Also I changed the original design from a standing lamp to a laying one. I though it would look nicer and lighten a wall much better. Also the potentiometers and buttons were better accessible (It’s not a bug it’s a feature).

Everything looked fine but a random flashing which disturbed me. One user from the forum gave me hint that there could be something wrong with my code since he had the same problem in the past. So glad this guy read the post -> no flickering anymore!

During the project i learned a lot about the whole progress of implementing a vision. I really started with a vague idea and then made use of a great toolchain (3d printing, uC development, board layout in eagle, testing testing and testing with oscilloscope)… Also i must say that there were a lot of coincidences like a broken diode and a wrongly connected mosfet which i eventually hadn’t noticed in other places… Maybe you (Kenzie) remember that i was happy about the one day on skype? Yeah, i figured the board was actually working … so this was my last week; for you! be happy!

Looking forward to party in fading colors!

Reference: (code and board layouts as well as the openscad 3d designs) (thread on rgb space and flashing issue)

App development

Ratelet a OSX Menulet to control iTunes’ rating and volume

Hey, i finally submitted my first official App to the Apple App Store! Check it out 😉

Ratelet is shown in your statusbar and gives you easy access to the rating of the current song and iTunes volume.

Ratelet will automatically hide itself from the statusbar when iTunes is not running and reveal if iTunes starts. You can hide the volume slider if you only need the rating bar.

Quickly changing iTunes volume can be useful especially when you use Airplay or output sound digital because the Mac’s volume buttons only affect analog output volume!





App development

NSStatusItem highlight custom Button workaround!

I’m building a Menulet with multiple buttons. Since these buttons do no inherit the default behavior of getting blue when clicked, i searched for a quick solution. Since i don’t know how to implement a custom cell and had no ambition to do so, i finally found a nice and quick solution.

The following lines make shure the button won’t show a white background which is default for every buttonType but NSMomentaryChangeButton.

  1. //make sure button won’t show the “alternateImage” when selected
  2. [configureItem setButtonType:NSMomentaryChangeButton];
  3. [configureItem setBezelStyle:NSRegularSquareBezelStyle];
  4. [configureItem setBordered:NO];

Then the next few lines are called in the MenuItem’s delegate every time the menu will open or close.

  1. #pragma mark menu delegate
  2. (void)menuWillOpen:(NSMenu *)menu {
  3.         [[configureItem cell] setBackgroundColor:[NSColor selectedMenuItemColor]];
  4. }
  5. (void)menuDidClose:(NSMenu *)menu {
  6.         [[configureItem cell] setBackgroundColor:[NSColor clearColor]];
  7.     [menu release];
  8. }