필터 지우기
필터 지우기

Why is it showing an "Unrecognized function or variable 'sample'" error code?

조회 수: 1 (최근 30일)
Zachary Oliver
Zachary Oliver 2020년 4월 2일
답변: sugga singh 2021년 2월 28일
%Sample and Alias 2
clear
clc
close all
% Define Sampling Frequencies to be Observed
Fs = [8000, 5000, 4000, 3300, 3000, 2700];
%% Create Array Ts such that Ts = 1 / Fs
Ts = [1/8000, 1/5000, 1/4000, 1/3300, 1/3000, 1/2700];
%Number of Frequency Copies (Must be Odd)
freq_copies = 9;
%Titles of Figures
graph_title = ["Fs = 8000 Hz";"Fs = 5000 Hz";"Fs = 4000 Hz";...
"Fs = 3300 Hz";"Fs = 3000 Hz";"Fs = 2700 Hz"];
%% Generate For Loop Code
% This for loop loops through 6 different sampling frequencies and plots
% the fft spectrum of each time sequency. It then creates a 2x3 subplot
% of the different fft spectrums
for k = 1:6
%Create 2D array, x, of size [N, K] where N is the number of points in a
%specific time sequence and K is the kth sample rate
%In other words, every column of x is a different sampled version of
%x(t) with a different sample rate
k
x(:,k)=sample(Ts(k));
%Take FFT of kth x_n and then create freq_copies number of copies label
%this variable (copied version of fft(x)) X
X(:,k)=repmat(abs(fftshift(fft(x(:,k)))),freq_copies,1);
%Create indexed frequency variable for x axis of plot (given)
f(:,k) = -(freq_copies)*Fs(k)/2:(freq_copies)*Fs(k)/(length(X(:,k))-1):(freq_copies)*Fs(k)/2;
%Generate subplot entry for each result
subplot(2,3,k)
%plot the kth column of f vs the kth column of X
plot(f(:,k),X(:,k));
%set x limits to make plot more clear
xlim([-6000 6000])
%Label Axis
xlabel('Frequency (Hz)')
ylabel('|X(f)|');
%Title graph with kth value in graph_title
title(graph_title(k))
end
  댓글 수: 1
Joe Privacy
Joe Privacy 2021년 2월 27일
편집: Joe Privacy 2021년 2월 27일
xlim([-6000 6000]) is wrong. Sampling frequency dictates that your max fft frequency is 4000, or else you'll have aliasing problems.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2020년 4월 2일
MATLAB does not define any function named sample
Note that Ts(k) is a scalar, so you would be calling sample() passing in just a scalar, so it is not clear what you are sampling.
Question:
% Define Sampling Frequencies to be Observed
Fs = [8000, 5000, 4000, 3300, 3000, 2700];
%% Create Array Ts such that Ts = 1 / Fs
Ts = [1/8000, 1/5000, 1/4000, 1/3300, 1/3000, 1/2700];
Wouldn't it be safer to define Ts = 1./Fs; ?

sugga singh
sugga singh 2021년 2월 28일
nice

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by