Sunday, October 31, 2010

Animated sine wave addition

My son is studying physics at school and one of topics was "waves". The physics study book he is using is printed and so doesn't include any wave animations. I thought animations would help with understanding but couldn't find any on-line animation where the user could change the wave frequency, phase.

I've wrote a WPF application where the user could control the frequency, phase of two sine wave and see the result of adding these two wave together. I then ported this to silverlight (which required a few small changes to the code) and published the result on

Since then, I've added the ability to also change the wave amplitude.

Sunday, October 24, 2010

WPF DocumentViewer

The WPF DocumentViewer control has some annoying features.

1. A find control ("Type text to find") that doesn't work.
2. A print function with limited configuration.

I work around these features by subclassing the DocumentViewer as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows;
using System.Printing;
using System.Windows.Xps;

namespace FixedDocPrinting
public class PrintDocumentViewer : DocumentViewer
PageOrientation _pageOrientation = PageOrientation.Portrait;
public PageOrientation PageOrientation
get { return _pageOrientation; }
set { _pageOrientation = value; }

Visibility _findControlVisibility = Visibility.Collapsed;
public Visibility FindControlVisibility
return _findControlVisibility;
_findControlVisibility = value;

private void UpdateFindControlVisibility()
object toolbar = this.Template.FindName("PART_FindToolBarHost", this);
ContentControl cc = toolbar as ContentControl;
if (cc != null)
HeaderedItemsControl itemsControl = cc.Content as HeaderedItemsControl;
if (itemsControl != null)
itemsControl.Visibility = FindControlVisibility;

public PrintDocumentViewer()
Loaded += new RoutedEventHandler(PrintDocumentViewer_Loaded);

void PrintDocumentViewer_Loaded(object sender, RoutedEventArgs e)

protected override void OnPrintCommand()
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

printDialog.PrintTicket.PageOrientation = PageOrientation;

if (printDialog.ShowDialog() == true)
// Code assumes this.Document will either by a FixedDocument or a FixedDocumentSequence
FixedDocument fixedDocument = this.Document as FixedDocument;
FixedDocumentSequence fixedDocumentSequence = this.Document as FixedDocumentSequence;

if (fixedDocument != null)
fixedDocument.PrintTicket = printDialog.PrintTicket;

if (fixedDocumentSequence!= null)
fixedDocumentSequence.PrintTicket = printDialog.PrintTicket;

XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);

if (fixedDocument != null)
writer.WriteAsync(fixedDocument, printDialog.PrintTicket);

if (fixedDocumentSequence != null)
writer.WriteAsync(fixedDocumentSequence, printDialog.PrintTicket);

This hides the find control and gives me the ability to print in Landscape.

To use this code, you will need to add a reference to the ReachFramework dll which contains useful classes in the System.Printing and System.Windows.Xps name spaces.

Thursday, July 8, 2010

Compiling Matlab mpgwrite with VS2010 for 64 bit Windows

I was having trouble producing Matlab movies of reasonable quality on my 64 bit Windows 7 laptop until I was able to compile and run mpgwrite (source available from Matlab Central

It wasn't obvious how to get this to compile and run so in case someone else would like to do this I've documented what I did:

1. Ensure Visual Studio 2010 installation includes the "X64 Compilers and Tools" (and option under the Visual C++ feature in the Visual Studio 2010 installer).

2. Configure the mex script for VS2010.
Matlab R2009b and earlier doesn't directly support Visual Studio 2010 but you can still use it by running
mex -setup
from Matlab.
a. Answer n to the first question (Would you like mex to locate installed compilers [y]/n? )
b. Select "Microsoft Visual C++ 2008 SP1 " (option 5)
c. The script will output:
The default location for Microsoft Visual C++ 2008 SP1 compilers is
C:\Program Files (x86)\Microsoft Visual Studio 9.0,
but that directory does not exist on this machine.
Use C:\Program Files (x86)\Microsoft Visual Studio 9.0 anyway [y]/n?

Answer n
d. Locate your Visual Studio 2010 installation folder and enter this in response to the next question. Visual Studio 2010 was probably installed to C:\Program Files (x86)\Microsoft Visual Studio 10.0
e. Finally, you will be asked to confirm the settings and Matlab will create a file mexopts.bat with the configuration information.

3. From Microsoft Visual Studio 2010\Visual Studio Tools open "Visual Studio x64 Win64 Command prompt (2010)".

4. Change to the src folder of the downloaded the mpgwrite source code.

5. Edit the file makefile and replace $(MCC) $(MCFLAGS) after the call to mex with
-v -DWIN32
That is, your makefile should contain:

mex -v -DWIN32 mpgwrite.c mfwddct.c \
postdct.c huff.c bitio.c mheaders.c iframe.c \
pframe.c bframe.c psearch.c bsearch.c block.c \
mpeg.c subsampl.c jrevdct.c frame.c fsize.c

5. Run
nmake -f makefile

6. This should generate the file
Copy this file to a folder on the matlab path.

You should now be able to generate mpeg movies from matlab.

% Mpeg generation example
clear F;
t = (0:1000)./10;
h = line( ...
'color','b', ...
'LineStyle','-', ...
'erase','xor', ...
'LineWidth',0.5, ...
for k=1:100
set(h,'ydata', sin(t-k).*t )
F(k) = getframe(gca);

Friday, July 2, 2010

Outlook 2010 fails to retrieve email from pop3 server

After upgrading from Outlook 2007 to Outlook 2010 we were unable to connect to pop servers on port 110.

A network monitoring utility (SmartSniff) showed that Outlook 2010 starts it's communication with a pop3 server by issuing a CAPA command. This command is supposed to retrieve a list of the server capabilities but when issued on the our network the TCP connection is immediately dropped when CAPA is sent. This can be seen by starting a telnet session with the pop server and sending the CAPA command.

Further investigation showed that Cisco routers can be configured to block invalid pop3 commands and that the router considers CAPA to be an invalid command.

The Cisco router has probably been configured to do this by running a variation of the following:

ip inspect name inspection-name pop3 [alert {on | off}] [audit-trail {on | off}] [reset] [secure-login] [timeout number]

We should just need to turn off the inspection of pop3 packets to resolve this problem.