函数Matlab定义中的问题

时间:2015-07-20 17:03:08

标签: matlab function definition

我在尝试转换函数中的算法时遇到了一些问题。这是我的算法:

N = 256;

F = 10;

Fs = 200;

for n=1:N % generate saw tooth test data � DELETE IF USING OTHER DATA

% signal is given by sin ( 2 * pi * F * t)

% t is sampled as t = nT = n/Fs, where T is sampling Time and n is nth sample and Fs = ACDs sampling frequency

% signal = sin ( 2 * pi * F * nT)

% signal = sin ( 2 * pi * (F / Fs) * n)

% so by above theory, if signal is 10 Hz and samped at 40 Hz.

% also other signals are added that are multiple of F or 10 Hz but of lower amplitude.

% signals are superimposed.

r(n) = 1.1;

r(n) = r(n) + sin(2 * pi * F * n / Fs);

r(n) = r(n) +  .5 * sin(2 * pi * 2 * F * n / Fs);

r(n) = r(n) +  (1. / 3.) * sin(2 * pi * 3 * F * n / Fs);

r(n) = r(n) +  .25 * sin(42 * pi * 4 * F * n / Fs);

r(n) = r(n) +  .2 * sin(2 * pi * 5 * F * n / Fs);

r(n) = r(n) +  (1.0 / 6.0) * sin(2 * pi * 6 * F * n / Fs);

r(n) = r(n) +  (1. / 7.) * sin(2 * pi * 7 * F * n / Fs);

end% if you print r[n], you�ll see that the saw tooth goes a little negative at the end

% plot(r);

grid on;

N1 = [ 0 : N - 1] / N;

N1 = N1 * Fs;

Y = fft( r , N); % take N point FFT.

subplot( 2, 1, 1);

plot( N1, r);

subplot( 2, 1, 2);

plot( N1, abs(Y));

好吧,我想把它变成一个功能,所以我尝试了这个:

F = 10;
Fs = 200;

function Fu(F,Fs)
 N = 256;

  for n=1:N % generate saw tooth test data � DELETE IF USING OTHER DATA

% signal is given by sin ( 2 * pi * F * t)

% t is sampled as t = nT = n/Fs, where T is sampling Time and n is nth sample and Fs = ACDs sampling frequency

% signal = sin ( 2 * pi * F * nT)

% signal = sin ( 2 * pi * (F / Fs) * n)

% so by above theory, if signal is 10 Hz and samped at 40 Hz.

% also other signals are added that are multiple of F or 10 Hz but of lower amplitude.

% signals are superimposed.

  r(n) = 1.1;

  r(n) = r(n) + sin(2 * pi * F * n / Fs);

  r(n) = r(n) +  .5 * sin(2 * pi * 2 * F * n / Fs);

  r(n) = r(n) +  (1. / 3.) * sin(2 * pi * 3 * F * n / Fs);

  r(n) = r(n) +  .25 * sin(42 * pi * 4 * F * n / Fs);

  r(n) = r(n) +  .2 * sin(2 * pi * 5 * F * n / Fs);

  r(n) = r(n) +  (1.0 / 6.0) * sin(2 * pi * 6 * F * n / Fs);

  r(n) = r(n) +  (1. / 7.) * sin(2 * pi * 7 * F * n / Fs);

  end% if you print r[n], you�ll see that the saw tooth goes a little negative at the end

% plot(r);

 grid on;

 N1 = [ 0 : N - 1] / N;

 N1 = N1 * Fs;

 Y = fft( r , N); % take N point FFT.

 subplot( 2, 1, 1);

 plot( N1, r);

 subplot( 2, 1, 2);

 plot( N1, abs(Y));
end

但是我收到了这个错误:

在此上下文中不允许使用函数定义。

所以我查了一下,并说我无法在控制语句中定义一个功能,但我不这样做。

感谢您的帮助。

0 个答案:

没有答案