Why is it showing an "Unrecognized function or variable 'sample'" error code?
6 views (last 30 days)
Show older comments
%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 Comment
Joe Privacy
on 27 Feb 2021
Edited: Joe Privacy
on 27 Feb 2021
xlim([-6000 6000]) is wrong. Sampling frequency dictates that your max fft frequency is 4000, or else you'll have aliasing problems.
Answers (2)
Walter Roberson
on 2 Apr 2020
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; ?
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!