MATLAB Answers

Plotting frequency with different vector error

1 view (last 30 days)
Jack Song
Jack Song on 8 Sep 2020
Answered: Amrtanshu Raj on 11 Sep 2020
I am trying to plot a frequency graph with the center at the origin but I keep getting the error that the two vectors are different lengths. Is there anyway to fix this?

  3 Comments

Walter Roberson
Walter Roberson on 8 Sep 2020
What is size() of your independent variable? What is size() of your dependent variable? Which plotting routine are you using?
Jack Song
Jack Song on 9 Sep 2020
This is the Code Im using, im not sure what the sizes are. I tried to use the 'origin' function and the limits but it still didnt work. If you could look at it it would be great thank you.
fs =48000; %sampling frequency
Ts= 1/fs; %sampling duration
symbolrate = 1000; %transmitted pulses/second should be an integer divisor of fs
symbol_length = fs/symbolrate; %number of samples in one symbol
pulse = ones(1,symbol_length); %1 pulse hamm/hann delete 1
textin = input('Enter ASCII text for transmission: ' , 's'); % textin can be anything
data_input = dec2bin(double(textin)); %change txtin which is 10# to 2#
[number,length_data] = size(data_input);
data0 = reshape(data_input' , 1,number*length_data); %change the data of matrix to row vector
[number,length_data] = size(data0);
for i = 1:length_data %change 1 bit to 2bit for 4pam
data(i) = bin2dec(data0(i));
end
[number, length0] = size(data);
symbol = 2*data-1; % process of 2PAM need to change for OOK and for 4PAM
for i = 1: length0
for j=1:symbol_length
signal((i-1)*symbol_length + j) = symbol(i) * pulse(j);
end
end
index = -Ts/2: Ts: ((length0*symbol_length)-1)*Ts; %Change length of data
% spectrum_index = -fs/2:fs/2; ((length0*symbol_length)-1)*fs
% index = -Ts/2: symbol_length
freq=fft(signal);
freq=fftshift(freq);
figure(1);
% graph= zeros
plot(index, signal); %time delay in front of line 39
axis([0 1.5*length0*symbol_length*Ts -2 2]);
xlabel('time');
ylabel('amplitude');
title('basband transmission signal of 2PAM');
figure(2);
plot(abs(freq));
hold on
grid on
x=linspace(-200,200) ;
y=linspace(0,0) ;
plot(x,y,'k-') ;
plot(y,x,'k-') ;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
% PlotAxisAtorigin(index,signal)
xlabel('frequency');
% xlim([-10, 600])
ylabel('magnitude');
title('spectrum of 2PAM signal');
Walter Roberson
Walter Roberson on 9 Sep 2020
What input are you using? When I tried in R2020a on Mac and used 'hello' as the ASCII input, I had no problem.

Sign in to comment.

Answers (1)

Amrtanshu Raj
Amrtanshu Raj on 11 Sep 2020
Hi,
To get the desired shift so that the graph is symmetric about the origin, you can make the following changes in your code where you plot the freq’ variable:
sz = length(freq)/2;
k = [-sz:-1,1:sz];
plot(k,abs(freq));

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!