please can someone help me with running this script
显示 更早的评论
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
采纳的回答
更多回答(1 个)
Geoff Hayes
2015-2-15
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 个评论
Leon
2015-2-16
Geoff Hayes
2015-2-16
编辑:Geoff Hayes
2015-2-16
Leon - what are the dimensions of the arrays sig_left, sig_right, and x0? As x0 is initialized as
x0=(Rmax-RPS*t*track_skip).*cos(RPS*t*2*pi);
and t is an array, I suspect that x0 is not of the same dimension as sig_left and sig_right.
Leon
2015-2-21
Geoff Hayes
2015-2-21
Leon - you are trying to concatenate together matrices that do not have the same dimension. What are the dimensions (sizes) of x, z, and y?
Geoff Hayes
2015-2-24
Leon's answer moved here.
They are all 1323696x1
Geoff Hayes
2015-2-24
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?
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics and Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!