please can someone help me with running this script

2 views (last 30 days)
Hi there, I am new to matlab and I having trouble running this script. I was designed the emulate the motion of a record groove but i can't seem to get it to work. I import the audio data and set the time vector as required but it just returns
Function definitions are not permitted in this context.
Please help
sig_left=data;
t=(0:0.0000441:30);
function [vertex, faces; vertex_color] = LPsim(t, sig_left;
sig_right;track_skip; groove_depth, Rmax, rpm)
rpm=33.3333;
% 33 1/3 rpm industry standard
Rmax=0.1524e6;
% Maximum radius (at time t=0), in microns (6"=0.1524m)
groove_depth=50;
% Groove depth (in microns)
track_skip=150;
% Distance between tracks (in microns)
sig_right=sig_left;
% Assume monoaural input
RPS=rpm/60;
% Rotational speed of record (revolutions per second)
% Some pre-computed constants
cos45=sqrt(2)/2;
sin45=sqrt(2)/2;
% Pre-allocate memory for large vectors (speeds performance)
nVerts=length(t)*3;
vertex=zeros(nVerts, 3);
vertex_color=zeros(nVerts, 1);
faces=zeros((length(t)-1)*2, 4);
% Construct verticies
% We are using the following conventions:
% x: lateral position, from center of rotation
% y: position on the lacquer, from center of rotation
% z: depth, z=0 at the surface
%
% The record cutter revolves around on the (x-y) plane.
%
% Compute neutral stylus position
x0=(Rmax-RPS*t*track_skip).*cos(RPS*t*2*pi);
y=(Rmax-RPS*t*track_skip).*sin(RPS*t*2*pi);
% Mix in the signal
x=cos45*(sig_left+sig_right)+x0;
z=sin45*(sig_left-sig_right)-groove_depth;
% Transpose to place them in row order
x=x';
y=y';
z=z';
% Construct 3 vertices: one at the upper left corner of the V-groove,
% another in the bottom apex of the V-groove, and lastly, one for the
% upper right corner. Both upper corners have a height (z) of '0' by
% definition (where z=0 at the surface of the lacquer).
vertex(1:3:end-2, :)=[x-z, y, zeros(length(z), 1)];
vertex(2:3:end-1, :)=[x, y, z];
vertex(3:3:end, :)=[x+z, y, zeros(length(z), 1)];
% Color-code groove based on depth (leave all other vertices at '0'
% since they are set to be at the surface (hence the zeros(length(z))...)
vertex_color(2:3:end-1)=z;
% Connect verticies to form faces
% Each time point generates a triangle, and every 2 triangles generates a face
% Therefore, given 'n' time points, you will have (n-1)*2 faces
% It turns out that since the verticies are numbered in order, for a 3
% triangle setup you will have (where the verticies are numbered from left
% to right, front to back)
% [1 2 5 4]
% [2 3 6 5]
% [4 5 8 7]
% [5 6 9 8]
% [ . . . ] and so on
% Compute the number of faces
nFaces=(length(t)-1)*2;
% Determine the index for the first vertex of next-to-last triangle
ntl_vertex=(length(t)-1)*3-1;
% Vectorized output
faces(1:2:nFaces, :)=[1:3:ntl_vertex; 2:3:ntl_vertex+1; 5:3:ntl_vertex+4;
4:3:ntl_vertex+3]';
faces(2:2:nFaces, :)=faces(1:2:nFaces-1, :)+1;
Thanks for your help

Accepted Answer

Harold Bien
Harold Bien on 24 Feb 2015
Edited: Harold Bien on 24 Feb 2015
Hi Leon,
It's hard to tell from the posted source, but a few things of note. First, you have lots of places where you used ';' when it's supposed to be ',':
For example: "function...(t, sig_left, sig_right,..." I'm not sure how MATLAB will interpret ";" in the argument list of the function.
Second: pay attention to dimensions of vectors as noted above by Leon. You have t defined to be a column vector - it will be of dimension 1xN where N is number of samples. Your signal is from another variable "data" which I do not know the layout of the vector (and I suspect it may be in row order, i.e. of dimension Nx1). From the comments, I can tell that x, y, and z are expected to be in column vector format (1xN) as I transpose the variables to yield the row vector (Nx1) in the lines "x=x'; y=y'; z=z';"
The line where the error occurs expects x, y, and z to be row vectors of dimension Nx1 as they are concatenated as columns.
For you convenience, I'll attach below the source code from the supplemental info but in electronic format (<http://www.rsc.org/suppdata/lc/b4/b409478f/b409478f.pdf)>. Note again that you can just run the function as-is without editing LPsim.m using the following command-line syntax:
[vertex, faces, vertex_color]=LPsim(t, data, data);
% Test case data=randn(1,100); t=0:1:99; [vertex, faces]=LPsim(t, data, data);
Finally, I do have to apologize for the difficulties in using the code. This was during my early days of MATLAB and I should have been more explicit about the required dimensions (1xN) of the input arguments.
[Edit: In the transfer, there was an inadvertent line-break in the comments which break LPsim.m which I fixed and re-uploaded].

More Answers (1)

Geoff Hayes
Geoff Hayes on 15 Feb 2015
Leon - the error message is telling you have inserted some code before your function definition. Your above code is
sig_left=data;
t=(0:0.0000441:30);
function [vertex, faces; vertex_color] = LPsim(t, sig_left; sig_right;track_skip; groove_depth, Rmax, rpm)
The sig_left and t are inputs to your function and so should not be pasted into the file that has your function defined. The only lines of code that can precede a function definition are comments.
What you need to do is remove those two lines of code so that the first line of your LPsim.m file is
function [vertex, faces; vertex_color] = LPsim(t, sig_left; sig_right;track_skip; groove_depth, Rmax, rpm)
Then, in the Command Window, type
sig_left=data;
t=(0:0.0000441:30);
[vertex, faces; vertex_color] = LPsim(t,sig_left);
The remaining inputs appear to be initialized in the first few lines of the function and so could probably be removed.
Try the above and see what happens!
  6 Comments
Geoff Hayes
Geoff Hayes on 24 Feb 2015
Leon's answer moved here.
They are all 1323696x1
Geoff Hayes
Geoff Hayes on 24 Feb 2015
Leon - put a bepreakpoint at this line (the one that is throwing the error) and run your function. When the debugger pauses at this line, determine the size/dimension of each variable by typing the following in the command window
size(x)
size(y)
size(z)
What do you see?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!