Formula for plotting a half-circle waveform.

Have you ever wondered what the formula for that thing that looks like a sine wave, but is really just half circles? Well, take a look below for the Matlab code of how to generate and plot such a signal with a particular frequency. If you need help translating to another programming or graphing language, just let me know.

Also, a handy feature of this code is the ability to only plot the first N periods. Take a look! :-)

Fs = 100;                     % Sampling frequency
T = 1/Fs;
L = 1000;                     % Number of samples
t = (0:L-1)*T;                % Time vector

fs = 1;                       % Frequency of signal.
A = 1;                        % Amplitude of signal.
% Half-circle waveform:
r = 1/(fs*4);
y = (A/r)*((2*(mod(t,r*4) == mod(t,2*r)) - 1).*sqrt(r^2 - (mod(t,2*r)-r).^2));

% Regular sine waveform:
y2 = A*sin(2*pi*fs*t);

% Plot the waveforms.
numPeriods = 3;
tEnd = round(numPeriods/(T*fs));
plot(t(1:tEnd),y(1:tEnd),t(1:tEnd),y2(1:tEnd))

 

Timestamp to Human Readable Date

This is some simple code I cooked up to convert a unix timestamp into a human readable date. The code will work up to the year 2100, when it will incorrectly add a leap day. Anyway, I couldn’t find this anywhere on the Internet, so maybe someone else will find this useful.

void dateAndTimeFromTimestamp(int i)
{
   int days    = i/(24*3600);

   // How many 4-year segments do you have (going 365, 365, 366, 365 days)
   int year    = days/(365*4 + 1);
       year    = 1970 + year*4;

   // How much is left over when you divide days into 4-year segments
       days    = days%(365*4 + 1);
   if(days >= 365 && days < 2*365)
   {
      days -= 365;
      year += 1;
   }
   else if(days >= 2*365 && days < 3*365 + 1)
   {
      days -= 2*365;
      year += 2;
   }
   else
   {
      days -= (3*365 + 1);
      year += 3;
   }

   int seconds = i%(24*3600);
   int hours   = seconds/3600;
       seconds = seconds%3600;
   int minutes = seconds/60;
       seconds = seconds%60;

   int month;
   for(month=1; month <= 12; month++)
   {
	// 30 days has September, April, June, and November
	if((month == 4 || month == 6 || month == 9 || month == 11) && days >= 30)
		days -= 30;

	// Except for February, which has 28 days
	// (the 29 days on leap year already accounted for above)
	else if(month == 2 && days >= 28)
		days -= 28;

	// All the rest have 31
	else if(days >= 31)
		days -= 31;

        else
                break;
   }
   days++;

   printf("%d-%d-%d %d:%d:%d",month,days,year,hours,minutes,seconds);
}

Getting an epson printer to work on Mac OSX

Man, I spent about three hours trying to print to from my mac to a windows 7 shared printer. The first thing I learned is that Mac OSX does not like SMB shares or hostnames, so the Windows printer tab on the add printer dialog is pretty much useless. It never did find my shared printer, and I couldn’t even ping the hostname of my other computer. Then, after a good deal of frustration, I found out that the EPSON-provided Mac OSX drivers (on the CD and from the website) do not work. Some places on the Internet said it has something to do with a conflict with a previous installation, or the Apple-provided drivers, but I’m more inclined to think that the EPSON-provided drivers are just buggy. So, I decided to just go with some open-source drivers, and I was happy to see things working in no time! Here are the steps:

  1. On your Windows computer, make sure you have enabled printer sharing, and have selected a share name that does not have spaces or weird characters
  2. On your Mac, download Gutenprint for Mac OSX (http://gimp-print.sourceforge.net/MacOSX.php)
  3. Run the uninstall
  4. Run the install
  5. I restarted my computer, but you probably don’t have to
  6. Go to Settings
  7. Click Print & Fax
  8. Click the [+] under the printers and scanners list
  9. Click the Advanced tab
  10. From the Type dropdown, select LPD/LPR Host or Printer
  11. In the URL box, type “lpd://192.168.1.x/sharename” (of course, replacing 192.168.1.x with your windows computer’s local IP, and sharename with the shared name of the printer). Make sure you use an IP address, not a hostname like “labPC1″.
  12. From the Print Using dropdown, select “Select Printer Software…”
  13. Choose the Gutenprint version of the driver for your printer model (it should look something like Epson ActionLaser 1100 – CUPS+Gutenprint v5.2.7)

How to use Alt+Print Screen with VMWare or VirtualBox on Mac OSX

If you’ve ever had a Windows process or application you were trying to document using screenshots, chances are that you’ve used the Alt+Print Screen shortcut to capture a single window to the clipboard. If you have Windows running on a virtual environment like VMWare or VirtualBox under Mac OSX, you can still use the print screen key. Here’s how:

  1. Go to System Preferences
  2. Click on Keyboard
  3. Check the “Use all F1, F2, etc. keys as standard function keys” box on the Keyboard tab.
  4. Use the F13 key as your Print Screen key, and the option key (alt) for your alt.  For example Alt+Print Screen would translate to option+F13

Manipulate PDF documents with JavaScript

So, after splitting each page of the document into two pages, I had another problem. Somehow, the pages had gotten out of order. Don’t ask me how it happened, but I needed to swap every other pair of pages, for a ~400 page document.

Turns out, Acrobat provides a scripting language (uses JavaScript syntax) that allows you to create buttons that do something, forms that auto-complete, and most importantly (for me) batch scripts that create or modify documents.

Basically, I read this and this, then I pasted the following into the interactive debugger, and hit ctrl-enter. If I had ever wanted to do this on multiple files, I could have saved it as a batch script:

var filename = this.path;
console.println("filename = "+filename);
for ( var i = 4; i < this.numPages; i+=4 )
{
   this.insertPages(i,filename,i+2);
   this.deletePages(i+3);
   console.println("Pages "+i+"-"+(i+3));
}