The Attic

A Blog of Tips

Subscribe to RSS feed

Mathematica - Generate a piecewise linear approximation function

For Mathematica: this generates a piecewise linear function from naïvely drawing lines between set of points {{x1,y1},{x2,y2},...}. Why is this useful when we have things like InterpolatingFunction? For the single reason that the returned function can then be combined with ordinary functions in a single instance of Plot[], instead of having to Plot[] and ListLinePlot[] and then Show[] them together, breaking the useful formatting and legends you may have added.
PieceLine[points_] := Module[{pp := points, pairs},
  pairs := Partition[pp, 2, 1];
  Return[Function[x,
    Evaluate[Piecewise[
      {
        InterpolatingPolynomial[#1, x], 
        x >= #1[[1]][[1]] && x < #1[[2]][[1]]
      } & /@ pairs
    ]]
  ]]
]


Usage:
ps = {{0,1},{1,1.5},{2,2},{3,2.25}}
functionOfPs = PieceLine[ps]
Plot[{functionOfPs[x],(x/2)+1,{x,0,4}]

Properly import ratings from Winamp or iTunes into foobar2000

, , , ...

Mucking around with foobar2000 after I got fed up with Winamp - looks very nice! however a slight issue: it's a pain in the butt to get ratings across. Even though Winamp can save some ratings in tags, it doesn't work for all formats and the scale is actually dependant on the container (i.e. some are tagged from 1-5, others from 1-100). There are a few methods around for doing this but most are quite nasty in that they require creating a list of thousands of songs in exactly the same order in both programs, then using a dodgy parsing method and hoping for the best...

parse.py

I didn't feel too confident with that so I wrote this python script. It parses an iTunes library XML file (as exported from iTunes or Winamp), and calls foobar appropriately.
Originally it output a batch file which could then be double-checked for correctness, but that broke on fïlés likè thêsé because Windows 7 STILL doesn't support Unicode characters in batch files. Python now calls Foobar directly.

It requires Python 3 for the same reasons - Python 2 does a hash job of dealing with Unicode. It should run with Python 2 but it will not work for any files with non-ASCII characters in their names. You will also need the Playback Statistics and Run Command components installed in Foobar.

You will have to edit it to specify where to find foobar2000.exe (remember to use forward slashes in the path) and also the location of your library XML file.

It has to run in batches because there's a limit on the size of arguments passable in the windows API (i.e. it can't send 10,000 filenames at once) - run parse.py from a command window, wait for foobar to settle down, approve "are you SURE you want to change the ratings in three billion and a half songs?", then again wait for foobar to settle down, then press any key in the command window to continue. My 3000-song library takes about ten passes to get through.

tl;dr:
1) install Python 3, and the Run Command and Playback Statistics Foobar2000 components
2) export XML from iTunes/Winamp
3) download parse.py, change the lines at the start so it knows where foobar2000 and your exported XML are
4) run parse.py, when it says wait for foobar2000 to settle IT MEANS IT, if you do not then it will only import one batch of ratings
5) enjoy your ratings smile

parse.py

Windows 7/Vista Startup Repair Loop of Horror

Seen it a few times on customers' boxes, but only recently had the damn thing happen to myself: Windows 7 BSODs on boot for some reason, and decides that whatever F8 option you choose, it's going to do a startup repair (and inevitably fail, and reboot, and the cycle continues). The underlying BSOD I believe can be for any cause, but there's no way of finding out - even the staple "disable automatic restart on system failure" simply invokes more startup repair fail. After years of being stumped, I finally worked out how to go about this: get a Windows 7/Vista dvd, and boot to it; once you get to the "Install Now?" screen, hit Shift+F10 to get a command prompt (getting a prompt from the "automatic repair failed, try Advanced Startup Repair" screen works equally well).
Now, assuming you have only the one Windows installation:
X:\>bcdedit /set {default} RECOVERYENABLED false

Now reboot.
You can now use "disable auto restart on sys failure" to actually see what's going on and fix the problem (in my case, pci.sys had been corrupted because el cheapo jarrad bought an el cheapo ssd that seems to be failing just sporadically enough as to make an RMA difficult).

Symbian S60 Running Slow

My Nokia E55, running Symbian S60 v3.2 I believe, has been running stupidly slow for the past couple of days, contacts app would freeze/hang up when I tried to open it, messaging took around three minutes to load up when I wanted to see what someone had just texted me... on a whim, I hooked it up in Mass Storage mode and chkdsk'd the microSD card. Cue win.

Bloody Norton

, , , ...

I'm no fan of Symantec's consumer products to begin with, but this just takes the case.

I had to run a routine repair install on a customer's box, but at login when Windows said it needed to be activated, the activation wizard (msoobe /a) never came up - Windows just sat there at the desktop... and sat there... and sat there... I left it overnight as this bloke had success with, but no luck, it was still just sitting there in the morning. I tried booting to Safe Mode and msconfig-ing a diagnostic startup (as you can't activate directly in Safe Mode), but still no luck. Just for shits and giggles, I installed an Anti WPA crack and logged in to the desktop under normal mode - on shutting down from this, I noticed Norton AntiVirus warning me about shutting down with a floppy in the drive (there was no frigging floppy drive at all in the computer, so I still have no clue what that was about). I ran the Norton Removal Tool (standard procedure and one of the most often executed files on our server you see), rebooted, removed the activation crack, ran msoobe /a, and lo! and behold, it activated fine. This completely cements my derisive attitude towards Symantec products.

PCI card drivers

, , ,

Ever change a motherboard and have Windows suddenly forget it actually already has the drivers for all PCI[express] cards installed? But it insists it needs the CD? And of course the CD has been thrown out/eaten by the dog/shattered, so no deal there?
Never mind, just point it to c:\Windows\system32\drivers when it asks where can it find them.

Bloody Windows.

Bloody Windows

, , , ...

So, I installed XP on a box with the SATA controllers to IDE mode (no floppy drive). All fine, drivers install well, so went through with activation.
I shoved in a SATA card to run the hard drive off while I installed the ACHI-mode SATA drivers (if the controller is in AHCI mode with no drivers, your system can't boot, catch-22 etc), again, after a little farting around, all is working fine off the AHCI-mode SATA controller. Windows doesn't complain at all.
Now it's an nForce board and I've had problems with the CD-provided SATA drivers and SATA DVD drives, so I decide to grab the newest driver package and install it. As you may be aware, the installer wanted to remove the old ethernet drivers and reboot before it would proceed, so I let it do just that. But hang on. After all this buggerising around before, which Windows happily took without complaint, it now only takes a simple ethernet driver uninstallation to trigger "You've changed hardware, three days to activate kthxbye". I know about the points triggering system, but I thought this reset with each reboot?
Fucking Windows.
May 2012
S M T W T F S
April 2012June 2012
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31