# Resolving reduced resolution issues in WSMLink

I have been attempting to evaluate Wolfram System Modeler (WSM) and it has not been going smoothly. It looks like some of the Modelica Models (combiTable1D for instance) supplied are not complete or not functional in WSM. I am working with Wolfram Support to get some clarification on that, but after days of trying I have been unable to get a simple output=input model working.

What I am trying to do as a first step is to just get an audio file into simulation and to port this straight through to an output to verify that it got in with reasonable accuracy. To do this I start with a very simple model:

model AudioProcessing
Modelica.Blocks.Interfaces.RealInput u=5;
Modelica.Blocks.Interfaces.RealOutput y;
equation
connect(u,y)
end AudioProcessing;


Here I have set the input to the constant 5 just to see if I can run the simulations without error and indeed this works fine.

In order to bring in the .WAV file I have tried many things, but the best results I get so far is to use WSMLink and the WSMInputFunctions option of WSMSimulate to provide and interpolated version of the .WAV to the simulation. For instance:

Needs["WSMLink"]
rawAudio = Import["C:\\Users\\vandel\\Desktop\\Mathematica\\Guitar_Sample.wav"]
samplePeriod = N[1/First[rawAudio][[-1]]];
sampleData = First[rawAudio][[1, 1]];
audioTimeSeries = Table[{n*samplePeriod, sampleData[[n + 1]]}, {n, 0, Length[sampleData] - 1}];
audiofunction = Interpolation[audioTimeSeries];
Plot[audiofunction[t], {t, 0, 10}, PlotRange -> All]


This plot looks fine and I can generate reasonable audio using the interpolation function.

Now I try to bring that into WSM as follows:

M = WSMSimulate[audioSignal, {0, 10}, WSMInputFunctions -> {"u" -> (audiofunction[#] &)}];


This runs fine but when I plot this as follows:

WSMPlot[M,"y", PlotRange -> All]


The resolution is so reduced that the sound can not be reproduced. The same takes place with the variable u and when I use the returned interploation table to build a new signal with higher time resolution such as:

{outputVector} = M[{"u"}];
testPlot = Table[outputVector[t], {t, 0, 10, .00001}];
ListPlot[testPlot, PlotRange -> All]


The plot from this looks just like the earlier plot.

I have tried using different solver methods and high numbers of InterpolationPoints and InterpolationOrder to no avail.

Any insight would be appreciated and if there is a better way to get a real world signal sample into the application I'd be happy to look at that as well.

-
Hello vandel, the site supports limited formatting using markdown. Please read the editing help page to format your post; you'll find it useful. I've edited it this time for you :) –  rm -rf Aug 28 '12 at 16:27
Hi Vandal. Congratulations with the first question tagged System-modeler here! There has been some discussion on Meta whether or not S-M questions are on-topic here. The jury is still out on this. For the time being, it should be OK, especially if you focus on the MMA/S-M integration. However, given the relatively newness of the product it may take a while before your questions are answered. Expertise needs some time to build up, so be prepared to wait a while. –  Sjoerd C. de Vries Aug 28 '12 at 18:41
I don't have a functioning version of WSM available so I have to guess. One problem may be that you're trying to plot more points with WSMPlot (I estimated it'll be a few 100,000 points) than is physically possible on any available screen, and I assume WSMPlot has to subsample considerably. I guess you have to show a much smaller part of the sample. –  Sjoerd C. de Vries Aug 28 '12 at 18:58
With "The resolution is so reduced that the sound can not be reproduced" you mean the plot cannot be reproduced or do you mean the sound itself? –  Sjoerd C. de Vries Aug 28 '12 at 19:00
Thanks Folks, I will read the editing help and use it in the future. With respect to the resolution being reduced, neither the sound nor the plot have the proper form. Note, the basic:</br> Plot[audiofunction[t], {t, 0, 10}, PlotRange -> All] </br> gives an accurate plot and using the original interpolation function generates and accurate sound. –  vandel Aug 28 '12 at 19:07

(Disclosure: I work at Wolfram Research developing Wolfram SystemModeler)

SystemModeler (and WSMLink in particular) does have some trouble with high frequency data, however, there are some things that can be done.

Using InputFunctions relies on automatic sampling of you input. As you know what frequency you want to sample the function at, using the "ModelicaCombiTimeTable" and the corresponding export format may be better. The model would look like this:

    model AudioProcessing
Modelica.Blocks.Interfaces.RealOutput y;
Modelica.Blocks.Sources.CombiTimeTable combiTimeTable1(fileName="C:\\Dev\\temp\\audio.txt", tableOnFile=true, tableName="invar");
equation
connect(combiTimeTable1.y[1],y);
end AudioProcessing;


Using the "audioTimeSeries" variable from your post, export to an input file, which will be read by the "ModelicaCombiTimeTable" component above:

Export["C:\\Dev\\temp\\audio.txt", {{"invar", audioTimeSeries}}, "ModelicaCombiTimeTable"]


For the given example, a fixed step solver may be better suited, as we know we want to sample at a given frequency. Set the step size to the known sample period. Also reduce the interpolation order to 1.

m = WSMSimulate["AudioProcessing", {0, 1.5},
Method -> {"Euler", "StepSize" -> samplePeriod}, InterpolationOrder -> 1];


When plotting high frequency functions, it is sometimes required to increase the number of initial sample points using PlotPoints. Use PerformanceGoal -> "Speed" to speed the plotting up (removes tooltips, for example):

WSMPlot[m, "y", PlotRange -> All, PerformanceGoal -> "Speed", PlotPoints -> 200]


Using a sample sound from ExampleData ("ExampleData/rule30.wav"), I was able to get a good result when playing the simulation result with the given sample rate:

Play[m[{"y"}, t][[1]], {t, 0, 1.5}, SampleRate -> 1/samplePeriod]
`

The slowness in reading high frequency simulation results remains, but is somewhat improved by these steps.

-
Thanks, this was really helpful –  Yehuda Jan 9 at 21:57