03-09-2009 06:03 AM
Hi
I have written MATLAB code to produce a squarewave at a certain frequency for 0.01 seconds. The code then loops back to produce a similar squarewave but at a different frequency, and then concatenates it to the end of the first squarewave. There are 100 such iterations such that it builds a squarewave of changing frequency that runs for 1 second (i.e. 0.01*100). I want to output this squarewave to the computer speakers.
The code works in MATLAB but when i copy it into the Matchscript window it does not. The commands that i am using seem to be compatible with Mathscript and it seems that the problem comes in at the for loop stage. My MATLAB code is pasted below and i have attached the LabVIEW VI with my Mathscript window. Could someone please have a look at my code and see if i am missing any common issues? I am a new LabVIEW user and am not entirely familiar with its intricacies.
______________________________________________________________________________
clc
sampleRate = 40000;
step = 1/sampleRate;
freqArray = [5247.819345 2691.190023 2379.575143 2242.862777 2162.605492 2105.101801 2056.973700 2011.841157 1966.260707 1918.191205 1866.327910 1809.784986 1747.934921 1680.325561 1606.638751 1526.672983 1440.340700 1347.674817 1248.840788 1144.151306 1034.080952 919.278207 800.572260 678.972385 629.941608 670.923135 706.090004 733.248856 750.729600 757.468401 753.055128 737.746864 712.449234 678.666661 638.422079 594.147026 548.544635 504.430794 464.562051 431.461673 407.256955 393.540721 391.268089 400.696344 421.371792 452.163428 479.456733 499.635983 519.683091 539.345489 558.379030 576.550816 593.641724 609.448655 623.786494 636.489853 647.414604 656.439263 663.466274 668.423222 671.264030 671.970137 670.551695 667.048751 661.532385 654.105760 660.331784 659.175196 658.042926 656.973398 656.008980 655.196461 654.587596 654.239768 654.216768 654.589753 655.438402 656.852357 658.933014 661.795790 665.573014 670.417674 676.508334 684.055673 693.311331 704.580088 718.236954 734.751698 754.724945 778.942900 808.463177 844.755078 889.940437 947.233277 1021.807261 1122.692006 1267.554308 1497.662150 1946.857495 4694.713066];
z = [ ];
t2 = [ ];
for n = 0:99
t = (0 + (0.01*n)) : step : (0.01*(n+1))-step;
y = (square(2*pi*freqArray(n+1)*t)+1)/2; % create squarewaves at different frquencies
z = [z,y]; % concatenate y arrays
t2 = [t2,t]; % concatenate time arrays
end
plot(t2,z)
sound(z,40000,16)
_______________________________________________________________________________
Solved! Go to Solution.
03-09-2009 06:06 AM
03-09-2009 01:10 PM
DCT,
Remember that array indices in MathScript are 1-based, so changing the for loop statement to "for n = 1:100" should make it work. This can be a little confusing because array indices are 0-based in native LabVIEW, so you need to make sure to add or subtract 1 when going back and forth between LabVIEW code and MathScript code.
Chris M
03-10-2009 01:06 AM