plotting x[n] with various points from a file

4 views (last 30 days)
So I have an assignment that requires to do this. I have only started on point number 1.
I also have a txt file named: 'signal.txt' that has a whole bunch of points like so:
5.2529
5.2530
5.2531
5.2534
5.2539
5.2544
5.2550
5.2558
5.2567
5.2577
5.2588
5.2601
5.2615
5.2630
5.2646
5.2663
5.2682
etc.....
  1. The input signal 𝑥[𝑛] is contained in a text file: signal.txt. The data format is “%1.4f\n” and there are 1028 samples. Write the code to read these 1028 values into a floating-point array. Plot the signal 𝑥[𝑛].
  2. Write code to calculate the 1028-point DFT of the signal of 𝑥[𝑛]: 𝑋1(𝑘)=Σ𝑥[𝑛]𝑒𝑗2𝜋𝑁𝑛𝑘𝑁−1𝑛=0
  3. Plot the magnitude and phase of 𝑋1(𝑘).
Below is my code, but what I am having trouble with is plotting x[n] (on point number 1 with the various points). I do not know how to really plot x[n]. I am confused on how long the horizontal axis is going to be and the compiler keeps throwing an error since the arguments inside stem() have to be the same size. Thank you and any help is appreciated.
% Jorge Avila - 1001543128
clc
clear
close all
% This project will require you to implement the DFT computation for a
% given signal in a MATLAB script and compare your output to the output of
% the MATLAB fft() function.
% formatSpec = '%1.4f\n';
formatSpec = '%f';
fp = fopen('signal.txt','r');
numberArray = fscanf(fp,formatSpec);
arraySize = size(numberArray);
% plot x[n]
subplot(2,2,1);
% maybe do a for loop????????
stem(1028, numberArray);
% really do not care what the x & y labels are.
xlabel('n and k values');
ylabel('x[n] and X[k]');
title('Plot of number generated');
% close the file pointer
fclose(fp);

Accepted Answer

Alan Moses
Alan Moses on 29 Oct 2020
Stem function requires both X and Y inputs to be vectors or matrices of the same size. You may use the following line of code:
stem(1:arraySize, numberArray);

More Answers (0)

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!