Plotting frequency with different vector error

조회 수: 1 (최근 30일)
Jackson
Jackson 2020년 9월 8일
답변: Amrtanshu Raj 2020년 9월 11일
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
Jackson
Jackson 2020년 9월 9일
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 2020년 9월 9일
What input are you using? When I tried in R2020a on Mac and used 'hello' as the ASCII input, I had no problem.

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

답변 (1개)

Amrtanshu Raj
Amrtanshu Raj 2020년 9월 11일
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));

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by