Bird
Raised Fist0
MATLABdata~10 mins

Filtering basics in MATLAB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to filter the vector x using the filter coefficients b and a.

MATLAB
y = filter([1], a, x);
Drag options to blanks, or click blank then click option'
Ay
Ba
Cx
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Using a as the first argument instead of b.
Swapping the order of arguments.
2fill in blank
medium

Complete the code to create a low-pass FIR filter with cutoff frequency 0.4 (normalized) and order 20.

MATLAB
b = fir1(20, [1]);
Drag options to blanks, or click blank then click option'
A0.2
B0.6
C0.4
D0.8
Attempts:
3 left
💡 Hint
Common Mistakes
Using a cutoff frequency higher than 0.5 (Nyquist frequency).
Confusing filter order with cutoff frequency.
3fill in blank
hard

Fix the error in the code to apply a moving average filter of length 5 to the signal data.

MATLAB
b = ones(1, [1]) / 5;
y = filter(b, 1, data);
Drag options to blanks, or click blank then click option'
A3
B5
C4
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a length different from 5 for the averaging vector.
Dividing by a number other than 5.
4fill in blank
hard

Fill both blanks to create a bandpass filter with order 30 and cutoff frequencies 0.3 and 0.6.

MATLAB
b = fir1([1], [[2] 0.6], 'bandpass');
Drag options to blanks, or click blank then click option'
A30
B20
C0.3
D0.4
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping cutoff frequencies or using wrong order.
Using cutoff frequencies outside the valid range.
5fill in blank
hard

Fill all three blanks to create a high-pass filter with order 25, cutoff frequency 0.5, and apply it to signal sig.

MATLAB
b = fir1([1], [2], 'high');
y = filter(b, [3], sig);
Drag options to blanks, or click blank then click option'
A25
B0.5
C1
D0.7
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong cutoff frequency or order.
Using denominator coefficients other than 1 for FIR filters.

Practice

(1/5)
1. What does filtering an array in MATLAB mean?
filtered = array(condition);
easy
A. Selecting elements that meet a condition
B. Sorting the array elements
C. Changing the array size randomly
D. Adding new elements to the array

Solution

  1. Step 1: Understand filtering concept

    Filtering means choosing only those elements from an array that satisfy a condition.
  2. Step 2: Analyze the MATLAB syntax

    The syntax array(condition) returns elements where the condition is true.
  3. Final Answer:

    Selecting elements that meet a condition -> Option A
  4. Quick Check:

    Filtering = Selecting elements [OK]
Hint: Filtering picks elements matching condition inside parentheses [OK]
Common Mistakes:
  • Thinking filtering sorts the array
  • Believing filtering changes array size randomly
  • Confusing filtering with adding elements
2. Which of the following is the correct MATLAB syntax to filter array A for values greater than 10?
easy
A. filtered = A > 10;
B. filtered = A{A > 10};
C. filtered = A(A > 10);
D. filtered = filter(A > 10);

Solution

  1. Step 1: Identify correct filtering syntax

    In MATLAB, filtering uses parentheses with a logical condition inside to select elements.
  2. Step 2: Check each option

    filtered = A(A > 10); uses A(A > 10), which correctly filters elements greater than 10.
  3. Final Answer:

    filtered = A(A > 10); -> Option C
  4. Quick Check:

    Use parentheses with condition for filtering [OK]
Hint: Use parentheses with condition to filter arrays [OK]
Common Mistakes:
  • Using > without parentheses returns logical array, not filtered values
  • Using curly braces {} which are for cell arrays
  • Using undefined function 'filter'
3. What is the output of this MATLAB code?
A = [3, 12, 7, 15, 9];
filtered = A(A >= 10);
disp(filtered);
medium
A. Error
B. [3 7 9]
C. [3 12 7 15 9]
D. [12 15]

Solution

  1. Step 1: Understand filtering condition

    The condition A >= 10 selects elements in A that are 10 or greater.
  2. Step 2: Apply condition to array A

    Elements 12 and 15 satisfy this condition, so filtered = [12 15].
  3. Final Answer:

    [12 15] -> Option D
  4. Quick Check:

    Elements >= 10 = [12 15] [OK]
Hint: Filter keeps elements where condition is true [OK]
Common Mistakes:
  • Including elements less than 10
  • Expecting original array output
  • Confusing logical array with filtered values
4. Identify the error in this MATLAB code for filtering values less than 5:
A = [2, 8, 4, 6];
filtered = A{A < 5};
medium
A. Using curly braces {} instead of parentheses ()
B. Missing semicolon at end of line
C. Incorrect comparison operator
D. Array A is not defined

Solution

  1. Step 1: Check filtering syntax

    Filtering arrays uses parentheses (), not curly braces {} which are for cell arrays.
  2. Step 2: Identify the error

    Using A{A < 5} causes an error because curly braces cannot index numeric arrays.
  3. Final Answer:

    Using curly braces {} instead of parentheses () -> Option A
  4. Quick Check:

    Use () for filtering numeric arrays [OK]
Hint: Use parentheses () to filter numeric arrays, not curly braces {} [OK]
Common Mistakes:
  • Using {} for numeric array indexing
  • Ignoring MATLAB error messages
  • Confusing semicolon necessity
5. Given a matrix M = [1 5 9; 4 2 7; 8 3 6], how do you filter all elements greater than 4 and return them as a single column vector?
hard
A. filtered = filter(M > 4);
B. filtered = M(M > 4); filtered = filtered(:);
C. filtered = M{M > 4};
D. filtered = M(:, M > 4);

Solution

  1. Step 1: Apply filtering condition

    Use M(M > 4) to select all elements greater than 4 from matrix M.
  2. Step 2: Convert result to column vector

    Use filtered(:) to reshape the filtered elements into a single column vector.
  3. Final Answer:

    filtered = M(M > 4); filtered = filtered(:); -> Option B
  4. Quick Check:

    Filter then reshape to column vector [OK]
Hint: Filter with condition, then reshape using (: ) for column vector [OK]
Common Mistakes:
  • Using curly braces {} for filtering
  • Trying to filter columns instead of elements
  • Using undefined function 'filter'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard