I need to read a PNG file and interpret all the information stored in it and print it in human readable format. While working on PNG, I understood that it uses CRC-32 for generating checksum for each chunk. But I could not understand the following information mentioned on the PNG file specification site:
The polynomial used by PNG is:
x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
Here is the link for reference:
http://www.w3.org/TR/PNG/
Can anyone please help me in understanding this?
http://en.wikipedia.org/wiki/Computation_of_CRC ?
According to list of CRCs in wiki, this polynomial (aka AUTODIN II polynomial ) is one of the most used.
CRC-32-IEEE 802.3 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1
Used in (Ethernet, V.42, MPEG-2, PNG, POSIX cksum, Arj, Lha32, Rar, Zip, and more..)
Rewritted with power marked by ^:
x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1.
So you can read source of cksum, e.g. here
http://www.opensource.apple.com/source/file_cmds/file_cmds-188/cksum/crc32.c
The 32-bit AutoDIN-II CRC is built
upon the following shift-register
reference model.
Polynomial: g(x) = 1 + x + x^4 + x^5 + x^7 + x^8 + x^10 + x^11 +
x^12 + x^1 + x^22 + x^23 + x^26 + x^32
Input data bit 0 first
Leading-zero checking is performed by the following procedure:
1. The crc register is initialized to 0xffffffff, not zero.
2. When a crc is appended, the 32 bits of the crc are inverted.
3. When checking a good message with an appended crc, the register
will return to the fixed value of 0xdebb20e3, rather than zero.
That's the CRC-32 algorithm implemented in zlib. Please don't implement your own when you can use that library instead.
[EDIT]: How to use the CRC calculator from zlib (an example in C extracted from the zlib docs).
#include <zlib.h>
uLong crc = crc32(0L, Z_NULL, 0);
while (read_buffer(buffer, length) != EOF) {
crc = crc32(crc, buffer, length);
}
if (crc != original_crc) error();
If you have the block of data that you want to get the CRC for, you don't need that while loop; you just get the initial value (first assignment to crc above) and then compute the value over the data that you have (second assignment to crc).
Every x term refers to a 1 in the binary representation of 0xedb88320, which is 11101101 10111000 10000011 00100000. The number on the left (least-significant) end, 1, is the (coefficient of the) constant (x^0) term. The next number from the left, 1, is the coefficient of the x term. The next number, 1, is the coefficient of the x^2 term. The next number, 0, is the coefficient of the x^3 term (which is absent because 0*x^3 = 0). And so on. There is an implied 1 to the right of the right (most-significant) end that is the coefficient of the x^32 term.
Related
how can I pretty print a transfer function in matlab?
H(s)=4.25/((0.3s+1)(22.5+1)(40s+1))
And this is how I've implemented it in matlab:
Hf=tf(K,conv(conv([T1 1],[T2 1]),[T3 1]));
But the result is :
Hf = 4.25/(270 s^3 + 918.8 s^2 + 62.8 s + 1)
1) How can I display it in the same format as in the first equation?
2) How can I display a controller in a PID format ?
Kp(1 + Ti/s + Td*s)
You are given a transfer function G(s)=1.81K(s+20)/(0.03338(s^3+10s^2+32s+32)). This system is connected with unity negative feedback . Determine the smallest positive value of K which makes the closed-loop system unstable. Give the answer to 3 d.p.
Correct Answer:
0.531 ± 0.02
This question is set by my lecturer and I have no idea how to do it.
The closed loop system will become unstable as soon as the rlocus function is no longer in the LHP. Either of the two lines when they intersect with 0 on the x-axis. Assume K=1 at first as we are multiplying by the gain.
>> G=tf([1.81 36.2],[0.03338 0.3338 1.0682 1.0682])
G =
1.81 s + 36.2
------------------------------------------
0.03338 s^3 + 0.3338 s^2 + 1.068 s + 1.068
Continuous-time transfer function.
>> rlocus(G)
you should see that when the real axis is 0 the gain is at 0.531.
if we want greater accuracy we can simply use the rlocfind(G,(point that it is 0)
A smart friend of mine showed me that "Routh Array" is the key for this equation.
Expand the polynomial as you have it, therefore you will have this:
VARS TERM VALUE
A s^3 0.03338
B s^2 0.3338
C s 1.06816 + 1.81Κ
D 1.06816 + 36.2Κ
Equate them as A*D=B*C and you will have the term "s^3" on both sides and then you can cancel it out and solve for K.
(0.03338)(1.06816 + 36.2K) s^3 = (0.3338)(1.06816 + 1.81K) s^3
0.0357 + 1.2084K = 0.3566 + 0.6042K
(1.2084 - 0.6042) K = 0.3566-0.0357
K = 0.5311155247
Otherwise use the rlocus function in MATLAB.
Im trying to design a PID compensator with Matlab.
I'm doing the following:
My plant has this transfer function:
plant
( 0.0195 s - 6.5 )/ (1.74e-06 s^2 - 0.003 s - 1)
So, using the PID tune function, specifying the desired crossover frequency:
>> [info,pid_c] = pidtune(plant,'PID',(2E6/6)*2*pi)
info =
Kp + Ki * 1/s
with Kp = 162, Ki = 1.96e+08
Continuous-time PI controller in parallel form.
pid_c =
Stable: 1
CrossoverFrequency: 2.0944e+06
PhaseMargin: 60.0000
But, when I close the loop and analyse the poles of the system I see that there's a pole in the right semi-plane:
>> pid_c = 162 + 1.96E8/s
pid_c =
( 162 s + 1.96e08 ) / s
Continuous-time transfer function.
>> sys=feedback(plant*pid_c,1)
sys =
(3.159 s^2 + 3.821e06 s - 1.274e09 )/ ( 1.74e-06 s^3 + 3.156 s^2 + 3.821e06 s - 1.274e09)
Continuous-time transfer function.
>> pole(sys)
ans =
1.0e+06 *
-0.9071 + 1.1721i
-0.9071 - 1.1721i
0.0003 <==== RSP pole
My intuition says I should get away with this pole by adding a (s+0.0003) to the numerator of the PID:
>> pid_c=pid_c*(s+0.0003)
pid_c =
( 162 s^2 + 1.96e08 s + 5.88e04 )/ s
But it doesnt work at all, and the tranfer function of my closed-loop goes like that:
sys =
(3.159 s^3 + 3.821e06 s^2 - 1.274e09 s - 3.822e05) / (3.159 s^3 + 3.821e06 s^2 - 1.274e09 s - 3.822e05)
and off course thats wrong.
I really appreciate your help.
The problem is because you are not calling pidtune correctly. The first variable returned is the controller, the second the information about closed-loop stability, not the other way round. From the documentation:
[C,info] = pidtune(...) returns the data structure info, which
contains information about closed-loop stability, the selected
open-loop gain crossover frequency, and the actual phase margin.
So I would change the code to:
[pid_c,info] = pidtune(plant,'PID',(2E6/6)*2*pi);
sys=minreal(feedback(pid_c*plant,1));
pole(sys)
The second error you make is that the pole isn't at 0.0003, but at 0.0003 * 1e6. If you are going to rely on what is displayed on the screen, you should at least use format long g to have more significant digits.
Also, note that I have used minreal to compute a minimum realisation of the closed-loop transfer function, as you seem to be having a mix of very large and very small number, not a good combination.
I am developing an equilibrium model where producers weight benefits and costs to choose the optimal time they work. However, the relevant benefit and cost functions are both rational, which results in a complex first-order derivative of the problem. When parameter values are introduced, I get systems of 3 equations in 3 variables just like the following:
eq1 = (10940*((12034*x)/35 + (1094*y)/5 + (2734*z)/25))/(7*(((12034*x)/35 + (1094*y)/5 + (2734*z)/25)^2 + 640000)) - (10940*((12034*x)/35 + (1094*y)/5 + (2734*z)/25)^3)/(7*(((12034*x)/35 + (1094*y)/5 + (2734*z)/25)^2 + 640000)^2) == x^3/5184
eq2 = (10940*((1094*x)/15 + (51418*y)/105 + (2734*z)/25))/(7*(((1094*x)/15 + (51418*y)/105 + (2734*z)/25)^2 + 640000)) - (10940*((1094*x)/15 + (51418*y)/105 + (2734*z)/25)^3)/(7*(((1094*x)/15 + (51418*y)/105 + (2734*z)/25)^2 + 640000)^2) == y^2/576
eq3 = (5468*((1094*x)/15 + (1094*y)/5 + (30074*z)/75))/(5*(((1094*x)/15 + (1094*y)/5 + (30074*z)/75)^2 + 490000)) - (5468*((1094*x)/15 + (1094*y)/5 + (30074*z)/75)^3)/(5*(((1094*x)/15 + (1094*y)/5 + (30074*z)/75)^2 + 490000)^2) == z^2/576
It has usually taken around 48 hs for MATLAB to find solutions for such systems of equations - when it does find them - using the solve command. Is there a more efficient way to complete this task? (I do have a pretty clear guess at what solutions should look like: all three outcome variables are expected to be within the [0,12] interval.)
First, I need to state that I'm not exactly a math lover or an avid Matlab user. This is college homework that I have been trying to solve for the last 3 hours with no luck. I have googled like crazy, even asked for help via Facebook (where I got replies, but nothing that solved my questions).
This is my first time using this software and I need to graph some things really quick. But no matter how much I read I keep getting syntax errors and the like.
Basically, I have this equation:
4*x^2 + y^2 + z^2 - 8*x + 2*y + 3 = 0
And I'm trying to plot it/graph it. I thought it would be very straightforward but I'm having problems doing it.
Google searches have shown me the functions ezplot() and ezsurf(), but:
When I do ezplot('4*x^2 + y^2 + z^2 - 8*x + 2*y + 3'), I get the following error:
??? Error using ==> char
Cell elements must be character arrays.
Error in ==> ezplot at 158
fmsg = char(f);
And when I do ezsurf('4*x^2 + y^2 + z^2 - 8*x + 2*y + 3')
The expression 4*x^2 + y^2 + z^2 - 8*x + 2*y + 3 must only have 2 symbolic variables
Error in ==> ezgraph3>surfplot at 526
[F,var] = ezfixfun(F,fargs,flabel);
Error in ==> ezgraph3 at 49
[dummy,h] = surfplot(f,domain,surfstyle,cax,Npts,fixdomain,flabel,fargs);
Error in ==> ezsurf at 65
h = ezgraph3('surf',args{:});
Again, this is really my first time using this software. The teacher didn't give us any explanation so I have no idea of what these errors are supposed to mean, let alone solving them. I have no idea of what I'm doing or what I am supposed to do.
I have tried using solve() to solve for 'z' first and then plot the two results I get.
s = solve('4*x^2 + y^2 + z^2 - 8*x + 2*y + 3', 'z')
s =
-(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)
(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)
But that's not working either. When I use ezplot() it tells me to use ezsurf, and when I use ezsurf() it tells me about a variable "h" that I have never even declared:
>> ezsurf('-(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)', '(- 4*x^2 + 8*x - y^2 - 2*y - 3)^(1/2)')
??? Undefined function or variable "h".
Error in ==> ezgraph3 at 66
hh = h;
Error in ==> ezsurf at 65
h = ezgraph3('surf',args{:});
Any help plotting that will be really appreciated. My head is going to explode because people have been telling me this is supposed to be really straightforward.
Thank you!
The following works for me:
ezsurf('(8*x - 4*x^2 - y^2 - 2*y -3)^(1/2)');
Note that ezsurf accepts a function of 2 dimensions (that maps into a 3rd dimension). So you must express the function in the form f(x, y) = blah, where blah is what you want as your input to ezsurf. All I did was re-arrange your function into the form z = blah, and then input blah to ezsurf.
Oh, and two other things, 1) The resulting graph only tells half the story, since the square root of z^2 can be z or minus z (by default, matlab takes the positive square root). 2) The function as above can only be analyzed without complex numbers (such as I've done here) for a very restricted domain of x and y.