How to change values sin table

7 views (last 30 days)
Tajinderpal Toor
Tajinderpal Toor on 1 Oct 2020
Commented: Sindar on 3 Oct 2020
So I understand that Sin(x) creates an array of values between -1 and 1. In the code below I am trying to use a for loop to adjust the values within the table so I can only take every delta'th value of the table. But I keep getting an error :Array indices must be positive integers or logical values. Below is my code.
% Problem: We want to use a lookup table to generate a sinusoid at a user
% specified frequency Fo while the DAC is operating at a sampling frequency
% Fs
% Design considerations: What are the advantages of generating a signal
% from a lookup table?
% Ask users for a table size and generate a sine table. The sine table will
% be an array of the specified size, and contain values for exactly one
% period of a sine
% Design consideration: Given that your Fs is 16000, and that you will be
% asked to create unique sinusoids at frequencies {250, 500, 750...
% 2000}Hz, what is the minimum table size possible?
% Matlab help search terms: input, sin, matrix indexing
clear all;
close all;
Ts= input('Enter a TableSize'); %Ts=tablesize
Ts1=0:Ts-1;
B=sin(Ts1)
% Ask users for a desired frequency and use the given Fs to calculate step
% size delta. check the lab presentation slides for delta
Fo=input('Enter your desired frequency'); %Fo=desired frequency
Fs = 16000;
delta=(Ts*(Fo/Fs));
% Use a for loop and plot to simulate the output being generated in real
% time
% Matlab help search terms: plot, Automatically Refresh Plot After Changing Data
x= [0 0] %0.004=65/16000
y= [0 0]
p = plot(x,y) % you need to fill in the arguments
xlim([0 1/250]); % assuming minimum frequency is 250Hz, this will get at least 1 cycle
ylim([-32768 32767]); % sinusoids go from -1 to 1. The TMS3206713 takes output from [-32768, 32767]
xlabel('Time(s)');
ylabel('Output');
p.XDataSource = 'x';
p.YDataSource = 'y';
% sin(x) gives you an array of sin values that are in the first cycle from
% -1 to 1, the value of x is the table size and will give you an array of
% that many elements, we multiplied by 32000 so that we can fit within the
% limit of the TMS.
for t = 2:65
x(t)=t/Fs
y(t)=32000*B(delta*t/Ts);
refreshdata;
drawnow;
end
  1 Comment
Sindar
Sindar on 3 Oct 2020
  1. put code in code blocks (button is in ribbon at top)
  2. copy the full error text
  3. you're probably passing a decimal number to a variable. One reason might be that you missed a '*' for multiplication

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!